Junit and Mockito - LearningRbcRegistry/Wiki GitHub Wiki

#Junit and Unit tests-
Antoine Bour / Nov 16*


### 1. Why ###

Different kinds of test approach exists (refer to waterfall vs Agile test pyramid)

  • Units tests
    • Is the application tested and deployable?
    • Test individually each functions/method.
  • Integration tests
    • Is the application integrated with its execution environment
    • Test the result of function A interactions with function B.
  • Acceptation tests
    • Is the application accepted by the end user?

We are interested here by the Units tests. For more info: Refer to Code Quality documentation

### 2. Unit Lifecycle###

Junit follows a specific sequence of events when invoking tests.

  • It constructs a new instance of the test case for each test method
    • If 5 tests method: 5 instances: No shared instances are allowed between test method.
  • For each test Method, call of setUp() Method (or @Before)
  • Calls the test method
  • Calls the tearDown() method (or @After)

### 3. How### ##### a. Principles#####

  • Respect code conventions

  • Easy and lisible

  • Test only one comportment

  • Call to dependances are to be limited

    • Easy and lisible.
    • Test only one comportment
    • Call to dependences are to be limited
    • Use mocks (object that simulate a real object -Db, webservice –).
  • A test case is composed of 3 objects: - A Start context - A prediction - An End context

  • Tests cases are based on assertions

  • TestSuite can be defined, they allow to regroup multiple test and indicate the execution order

##### b. One Mocking Framework: Mockito#####

  • Test a component that depends on other component but which is not yet developed
  • Unit test! We only test 1 class!
  • Infrastructures’ concerns that make impossible the testing
  • Real component are slow
  • Our test has to be the smallest possible. Why testing another linked class that may contain bug?

A Mock simulates the comportment from the classes used by the method/function that has to be tested.

Mockito allows:

Mock object Dummy implementation for a class or interface in which is defined the output of a certain method call
They can be created manually or through framework. ClassName mockClass = Mockito.mock(ClassName.class) Mockito can be used through Junit
Stubbing Controll class comportment and returned values. when mock.method()).thenReturn(..) We add a comportment to a method/function

Exemple:

stub(categoryService.getAllCategory()).toReturn(new ArrayList<Category>());
thenCallRealMethod Call real method. Do not Stub it.
@Spy Mock that wrap an object. An instance of implemented class. Using Spy, the real method is called.

##### c. Exemple:#####

##### d. Most used commands:#####

@Mock Shorthand for mock creation
inOrder.verify Verify the call order of functions/methods inside the tested function/class
Example: Project Mockito2, AuthenticatorApplicationTest.testAuthenticate
verifyZeroInteractions Make sure that there is no interactions between mocks
Stubbing consecutive calls when(mock.someMethod("some arg")) .thenThrow(new RuntimeException()) .thenReturn("foo");

On first call it will return a RuntTimeException, On second call it will return “foo”
Example: Project Mockito2, AuthenticatorApplicationTest.testAuthenticate, check Boolean actual/actual2

PartialMock Call the real method on specific mock method call
Verify Check that certain behavior happened the wished number of times. (Example Mockito2)
verifyNoMoreInteraction Checks if any of given mocks has an unverified interaction
verifyZeroInteractions Verifies that no interactions happened on given mocks
doThrow() Stubbing a void method with an exception
doAnswer() Stub the void method with a generic Answer
doReturn() Same than when()
doNothing() Ask Mokito to go in the method but without testing their content

##### e. In a Unit Test:#####

  • Create Objects
  • Create Mock objects
  • Stubbing
  • Calls the test method
  • Make assumptions
    • Asserts
    • Verify (exact 0/n call of wished method in the test)
    • Check call order inside tested function
    • Timeout

[...]

⚠️ **GitHub.com Fallback** ⚠️