JUnit

You use a Runner to invoke unit tests and test suites

Composing a suite of test classes

The Suite is a container used to gather tests

JUnit designed the Suite to run one or more test cases.

The test runner launches the Suite; which test case to run is up to the Suite. The test runner automatically creates a Suite if you don't provide one of your own.

The Suite object is a Runner that executes all of the @Test annotated methods
in the test class

@RunWith(value=org.junit.runners.Suite.class)
@SuiteClasses( value={FolderConfigurationTest.class,
FileConfigurationTest.class})
public class FileSystemConfigurationTestSuite {
}

You use a test suite to group related test classes together, allowing you to invokethem as a group. You can even group suites together in higher-level suites. 


@Before and @After

NOTES:

  • the annotated methods must be public
  • if you have more than one of the @Before/@After methods, the order of their execution is not defined.

 @BeforeClass and @AfterClass

To annotate your methods in that class. The methods that you annotate will get executed, only once, before/after all of your @Test methods.

NOTES:

  • The annotated methods must be public and static
  • if you have more than one of the @BeforeClass/@AfterClass methods, the order of their execution is not defined. 

Domain object

In the context of unit testing, the term domain object is used to contrast and compare the objects you use in your application with the objects that you use to test your application (test objects). Any object under test is considered a domain object.


test fixture

It is the pretest state. We set up a test by placing the environment in a known state (create objects, acquire resources).

All test methods can share the code, put it into the fixture without combining test methods.


Examples

NOTE
  • Before assertEqual/assertSame is a good practice to  verify an object isn’t null. We use the assertNotNull(String, Object) signature so that if the test fails, the error displayed is meaningful and easy to understand
    @Test
    public void testProcessRequest() {
        Response response = controller.processRequest(request);
        assertNotNull("Must not return a null response", response);
        assertEquals("", new SampleResponse(), response);
    }


    @Test
    public void testProcessRequestAnswersErrorResponse() {
        SampleRequest request = new SampleRequest("testError");
        SampleExceptionHandler handler = new SampleExceptionHandler();
        controller.addHandler(request, handler);
        Response response = controller.processRequest(request);

        assertNotNull("Must not return a null response", response);
        assertEquals(ErrorResponse.class, response.getClass());
    }