If you write propers tests for your code, you are less likely to find any bugs later down the road. Also if anything breaks later, it will be easier to find where the problem is.
http://en.wikipedia.org/wiki/Test-driven_development
Example
StringUtil.java
Code
public class StringUtil {
/**
* Reverse a string
* @param str
* @return
*/
public static String reverse(String str) {
String result = "";
for(int i = str.length() - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
/**
* Turn a string into all uppercase letters
* @param str
* @return
*/
public static String uppercase(String str) {
// did this on purpose
return str.toLowerCase();
}
}
StringUtilTest.java
Code
import junit.framework.TestCase;
public class StringUtilTest extends TestCase {
public void testReverse() throws Exception {
// valid tests
assertEquals("ih", StringUtil.reverse("hi"));
assertEquals("racecar", StringUtil.reverse("racecar"));
assertEquals("get it?", StringUtil.reverse("?ti teg"));
}
public void testUppercase() throws Exception {
// should fail because StringUtil#uppercase purposely puts it all to lowercase
assertEquals("HI", StringUtil.uppercase("hi"));
assertEquals("CAP", StringUtil.uppercase("CAP"));
}
}
Result:
http://prntscr.com/6amx2l
StringUtil#uppercase fails, so I will then go back into StringUtil.java and fix it so the test passes.