Skip to content

The main goal of this project is to explore basics od Mockito in Spring Boot environment.

Notifications You must be signed in to change notification settings

mtumilowicz/springboot-mockito

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

springboot-mockito

The main goal of this project is to explore basics od Mockito in Spring Boot environment.

manual

  • add @ExtendWith(MockitoExtension.class) to the class

  • mocking

    1. no args

      CustomerRepository customerRepository = mock(CustomerRepository.class);
      when(customerRepository.findAll()).thenReturn(Collections.singletonList(customer1));
      
    2. regardless argument

      CustomerRepository customerRepository = mock(CustomerRepository.class);
      when(customerRepository.findById(any(Integer.class))).thenReturn(Optional.of(customer1));
      
    3. specific argument

      CustomerRepository customerRepository = mock(CustomerRepository.class);
      when(customerRepository.findById(1)).thenReturn(Optional.of(customer1));
      
    • Note that: when(...).thenReturn(...) and doReturn(...).when(...) are equivalent for mocks, they differ when comes to spy:
      • when thenReturn - makes a real method call just before the specified value will be returned (if the called method throws an Exception you have to deal with it and you still get the result),
      • doReturn when - does not call the method at all.
  • to verify how many times method was invoked:

    1. once (no args)

      verify(customerRepository).findAll();
      
    2. twice (specific argument)

      verify(customerRepository, times(2)).findAll();
      
    3. at least once (regardless argument)

      verify(customerRepository, atLeast(1)).findById(any(Integer.class));
      
    4. at most once

      verify(customerRepository, atMost(1)).findById(2);
      
    5. zero

      verify(customerRepository, never()).findById(2);
      

tests

Coverage: 70%

About

The main goal of this project is to explore basics od Mockito in Spring Boot environment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages