Tests Unitaires

From My Limbic Wiki
Revision as of 16:06, 25 September 2020 by Fukakai (talk | contribs) (Page créée avec « =Before & After= ===@Before @After=== The method with this annotation is executed before or after every test method ===@BeforeClass @AfterClass=== The method with this ann... »)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

Stubbing

  • 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

Mock

  • 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.