Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Testing? JUnit?

So in a thread I read recently somebody was saying something about making tests with JUnit, and I've seen in some IDE's stuff for tests, can somebody maybe explain what they are, what they're for, how to use them, and and example?

Answers to one or any number of the above questions would be appreciated!
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.
So does the program stop if it doesn't pass? Cause the results didn't show anything about cap?
I made a thread about unit tests a while ago in the C# section, the site linked in the thread also have Java examples.

https://hackforums.net/showthread.php?tid=4598034


(Feb 27, 2015 08:32 PM)▁▂▃▄▅▆M1kep▆▅▄▃▂▁ Wrote:  So does the program stop if it doesn't pass? Cause the results didn't show anything about cap?

You will run those tests before you even run your program. This way you can prevent releasing a piece of software that does not what its expected to do. Also if your boss (if you would have one) asks you if the program does what he wants it to do you can show them the tests. (if done correctly ofcourse)

[Image: D12KBT6.png]
(Feb 27, 2015 08:32 PM)▁▂▃▄▅▆M1kep▆▅▄▃▂▁ Wrote:  So does the program stop if it doesn't pass? Cause the results didn't show anything about cap?

Unit tests should be small, precise and - in great numbers - cover everything possible to cover. They should be able to be run in the background all the time if need be. You shouldn't change the code in such a way that you break a test. If you really do need to make changes, you change the test to test that your new code-to-become works, and then you do the code.

This way, whenever the system is build, you can ensure that your code works exactly as expected.
[Image: TEGE4Zx.png]

Puzzle level: 2/5


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by HepicFeb 19, 2018 11:28 PM 2 124 Feb 19, 2018 11:28 PM
Last Post: Hepic
[Java] by legenda008Jan 30, 2016 04:12 AM 3 199 Jan 30, 2016 04:12 AM
Last Post: csrk
[Java] by SIM-2015410Oct 26, 2015 07:53 AM 2 224 Oct 26, 2015 07:53 AM
Last Post: Remix