Old Testing with JUnit4

This is now deprecated and you should use JUnit5.
  • These tests must not output anything to stdout or stderr (or it'll fail the build). Note that if the code under tests output logs, they need to be captured and possibly asserted. For example:
    ...
    /**
     * Capture logs.
     */

    @Rule
    public AllLogRule logRule = new AllLogRule();
    ...
    assertEquals("Error getting resource [bad resource] because of invalid path format. Reason: [invalid url]",
       this.logRule.getMessage(0));
    ...
  • If you're testing Components, use MockitoComponentMockingRule (its javadoc explains how to use it in details)
    • Example 1: Canonical use case
      public class DefaultDiffManagerTest
      {
         @Rule
         public final MockitoComponentMockingRule<DiffManager> mocker =
             new MockitoComponentMockingRule(DefaultDiffManager.class);
      ...
         @Test
         public void testDiffStringList() throws Exception
         {
             // Null

              DiffResult<String> result = this.mocker.getComponentUnderTest().diff(null, null, null);
      ...
    • Example 2: Example showing how to not mock one dependency being injected (ObservationManager is excluded in the example)
      @ComponentList({DefaultLoggerManager.class, DefaultObservationManager.class, LogbackEventGenerator.class})
      public class DefaultLoggerManagerTest
      {
         @Rule
         public final MockitoComponentMockingRule<DefaultLoggerManager> mocker =
             new MockitoComponentMockingRule<DefaultLoggerManager>(DefaultLoggerManager.class);
      ...
  • If you're testing non Components, just use pure JUnit and Mockito (you're on your own!)
  • Examples:
Tags:
Created by Vincent Massol on 2018/04/29 19:53
   

Get Connected