JUnit Overview - rsanchez-wsu/jfiles GitHub Wiki

Overview

JUnit is a testing framework that allows developers to write tests that will test a portion of your code. Once the test case is created it can be ran as many times as the developer needs. This saves valuable time since the developer doesn’t have to test this section of code again manually.

Unit Test Case

Tests a specific part of code and requires a known input and a known output. You verify whether or not your test passed using an assert method. These JUnit Test Cases are typically kept in their own test class but may need to be broken down into separate classes that test different sections. See list of Methods of Assert Class Below:

  • void assertEquals(boolean expected,boolean actual): checks that two primitives/objects are equal. It is overloaded.
  • void assertTrue(boolean condition): checks that a condition is true.
  • void assertFalse(boolean condition): checks that a condition is false.
  • void assertNull(Object obj): checks that object is null.
  • void assertNotNull(Object obj): checks that object is not null.

Example:

To test the following method

public double testAdd(int value1, int value2){
    double result = value1 + value2;
    return result;
}

In your test class you would write a Unit Test Case

@Test
public void testAdd(){
    assertEquals(5, testAdd(3,2));
}

This test would pass successfully because the expected value of 5 would be returned from passing the testAdd() method the integers 3 and 2.

Using annotations you can set up values before tests run or possibly closing file streams after they are finished. This allows the developer to execute things before or after each test runs as well as before or after all tests run. A list of annotations can be seen below:

  • @Test annotation specifies that method is the test method.
  • @Test(timeout=1000) annotation specifies that method will be failed if it takes longer than 1000 milliseconds (1 second).
  • @BeforeClass annotation specifies that method will be invoked only once, before starting all the tests.
  • @Before annotation specifies that method will be invoked before each test.
  • @After annotation specifies that method will be invoked after each test.
  • @AfterClass annotation specifies that method will be invoked only once, after finishing all the tests.

Naming Conventions

Typically a unit test's name is very specific (so feel free to make them wordy). For example, if you were testing a portion of your code that multiplies two numbers together, and you are wanting to ensure that multiplying two negative numbers returns a positive number a possible test case name would be:

testNegativeMultipliedByNegativeShouldBePositive()

This will help when running multiple tests. If that test fails the developer will know just by reading the tests name that a negative number multiplied by a negative number returned a negative number, so there is a problem.

Test Class

You can create a test class to test multiple methods in the Java class that you are writing your unit tests for. For example, if you are testing a class that creates users you can ensure that each method in the class is functioning like it should. If you set a username, first name, and last name you could test methods that may exist such as getUsername() and assert that the getUsername() method returns the username that you used, possibly in a @BeforeClass method that sets up a user before any of the tests have run.

Running Tests in Eclipse

After you have written a test and want to try to run it simply right-click on the file and select run as->JUnit test.

RunningTest

This is the results when 6 tests run and one fails. As you can see it tells you which test failed, which is an important reason why you should name your tests well.

Results

Adding Tests to Run with Ant

The Junit and Junit-dev branches have running JUNIT tests with ant implemented.

You will need junit.jar in the Apache Ant lib folder. See ant for where that folder is and download Junit from here .

To add more tests:

  1. Open build.xml in the main directory
  2. Ctrl/Command + f "These are the tests"
  3. To add more tests add:
    • Where testclass is the name of the JUnit Test Class you are wanting to run through ant. Don't forget to
      include edu.wright.cs as part of the class name.
    • Where outtxt is the name of the text file where the test results will be written
  4. These tests will automatically run when "ant clean test" is executed from the command line.