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.

    public AllLogRule logRule = new AllLogRule();
    assertEquals("Error getting resource [bad resource] because of invalid path format. Reason: [invalid url]",
  • If you're testing Components, use MockitoComponentMockingRule (its javadoc explains how to use it in details)
    • Example 1: Canonical use case
      public class DefaultDiffManagerTest
         public final MockitoComponentMockingRule<DiffManager> mocker =
             new MockitoComponentMockingRule(DefaultDiffManager.class);
         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
         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:
Created by Vincent Massol on 2018/04/29 19:53

Get Connected