Tests Unitaires

From My Limbic Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Configuration

In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation.

Before & After

@Before @After

The method with this annotation is executed before or after every test method

@BeforeClass @AfterClass

The method with this annotation is executed before or after every test class

Parametrize Test

  • On top of the test class @RunWith(Parameterized.class)
  • method annotated with @Parameter is passing Arrays.asList()

Mockito

Mocks vs Stubs = Behavioral testing vs State testing

Mockito does not allow Stubbid final & Private methods

Stubbing

Empty class, only return the expected

  • cannot be dinamically created from code

<source lang="Java"> MyMockedService myMockedService = new MyMockedService(); stub(myMockedService.methodToTest("param")).return("value"); </source>

Test lifecycle with stubs:

  • Setup - Prepare object that is being tested and its stubs collaborators.
  • Exercise - Test the functionality.
  • Verify state - Use asserts to check object's state.
  • Teardown - Clean up resources.

Stubbing Variations

  • Argument Matchers

Spy

A Spy is a Stub than is also collecting informations during the test

Used on legacy system where you don't really have access to the code or dependencies

    • How many times this method is called ?
    • Is this method is called at least once ?

Mock

Fullclass with implementation, same comportement as in real code.

  • Creating Objects that simulate the behavior of real objects
  • can be dynamically created from code - at runtime
  • offer more fonctionnalities than stubbing
    • How many times this method is called ?
    • Is this method is called at least once ?

<source lang="Java"> MyMockedService myMockedService = mock(MyMockService.class); when(myMockedService.methodToTest("param")).thenReturn("value"); </source>

Test lifecycle with mocks

  • Setup data - Prepare object that is being tested.
  • Setup expectations - Prepare expectations in mock that is being used by primary object.
  • Exercise - Test the functionality.
  • Verify expectations - Verify that correct methods has been invoked in mock.
  • Verify state - Use asserts to check object's state.
  • Teardown - Clean up resources.

Argument Matchers

Mockito extends Matchers and theses methods are available

  • any()
  • anyString()
  • anyList()
  • ...

Exceptions

<source lang="Java"> //To prevent from failing when an exception is thrown @Test(expected=RuntimeException.class) </source>

Annotations

@Mock

Mockito automatically creates a Mock of this class needs @RunWith(MockitoJunitRunner.class)

@InjectMocks

<source lang="Java"> // Every time we use TodoBusinessImpl we want to inject todoServiceMock @Mock TodoService todoServiceMock; when(todoServiceMock.retrieveTodos("dummy"))).thenReturn(todos); // todoServiceMock is injected as a constructor parameter TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoServiceMock) </source> Instead of previous code, mockito can help <source lang="Java"> // Every time we use TodoBusinessImpl we want to inject todoServiceMock @Mock TodoService todoServiceMock; @InjectMocks TodoBusinessImpl todoBusinessImpl ; when(todoServiceMock.retrieveTodos("dummy"))).thenReturn(todos); // Replaced with Injected // MocksTodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoServiceMock) </source>

@Captor

<source lang="Java"> //@Captor ArgumentCaptor .. lets see later </source>

@MockBean

Spring Mock Bean

PowerMock