Java Unit Testing

  • Typical, unit tests are created in a separate project or separate source folder to keep the test code separate from the real code. The standard convention from the Maven and Gradle build tools is to use:

    • src/main/java - for Java classes
    • src/test/java - for test classes
  • To define that a certain method is a test method, annotate it with the annotation: @Test

  • JUnit assumes that all test methods can be executed in an arbitrary order. Well-written test code should not assume any order, i.e., tests should not depend on other tests.

  • Using JUnit4

    • Defining test methods: @Test, @Before, @After, @BeforeClass, @AfterClass
    • Assert statements: These assert statements typically start with assert. They allow you to specify the error message, the expected and the actual result. An assertion method compares the actual value returned by a test to the expected value. It throws an AssertionException if the comparison fails.
    • JUnit test suite: If you have several test classes, you can combine them into a test suite. Running a test suite executes all test classes in that suite in the specified order. A test suite can also contain other test suites.
    • Disabling tests
      Assume.assumeFalse(System.getProperty("os.name").contains("Linux"));
      
    • Parameterized test: You mark a test class as a parameterized test with the @RunWith(Parameterized.class) annotation. You can use the @Parameter annotation on public fields to get the test values injected in the test.

      package testing;
      
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import org.junit.runners.Parameterized;
      import org.junit.runners.Parameterized.Parameters;
      
      import java.util.Arrays;
      import java.util.Collection;
      
      import static org.junit.Assert.assertEquals;
      import static org.junit.runners.Parameterized.*;
      
      @RunWith(Parameterized.class)
      public class ParameterizedTestFields {
      
          // fields used together with @Parameter must be public
          @Parameter(0)
          public int m1;
          @Parameter(1)
          public int m2;
          @Parameter(2)
          public int result;
      
          // creates the test data
          @Parameters
          public static Collection<Object[]> data() {
              Object[][] data = new Object[][] { { 1 , 2, 2 }, { 5, 3, 15 }, { 121, 4, 484 } };
              return Arrays.asList(data);
          }
      
          @Test
          public void testMultiplyException() {
              MyClass tester = new MyClass();
              assertEquals("Result", result, tester.multiply(m1, m2));
          }
      
          // class to be tested
          class MyClass {
              public int multiply(int i, int j) {
                  return i *j;
              }
          }
      
      }
      
    • JUnit Rules: Via JUnit rules you can add behavior to each tests in a test class. You can annotate fields of type TestRule with the @Rule annotation. You can create objects which can be used and configured in your test methods. This adds more flexibility to your tests. You could, for example, specify which exception message you expect during the execution of your test code.

  • Testing Exceptions: The @Test (expected = Exception.class) annotation is limited as it can only test for one exception. To test exceptions, you can use the following test pattern.

    try {
     mustThrowException();
     fail();
    } catch (Exception e) {
     // expected
     // could also check for message of exception, etc.
    }
    

    *

REFERENCE: http://www.vogella.com/tutorials/JUnit/article.html#whytesting

results matching ""

    No results matching ""