简体   繁体   中英

Spring Framework - Mock is not mocking

I have a Spring MvC app. (The Spring Framework is an application framework and inversion of control container for the Java platform) with this test:

    @RunWith(SpringJUnit4ClassRunner.class)
    public class AutorisationServiceTest {
    
        @Autowired
        @InjectMocks
        private IAutorisationService service;
    
        @Mock
        PersonneRepository personneRepository = Mockito.mock(PersonneRepository.class);
    
    
        @Before
        public void setup() {
        }
    
    
        @Test
        public void should_Find_GardeWith2Affectations() throws IOException {
    
            when(personneRepository.getAll(anyString())).thenReturn(DataLoader.mockData());
            service.getAll("rules");
        }
    
    }

    @Service
    public class AutorisationService implements IAutorisationService {
    
        private final PersonneRepository personneRepository;
    
        public AutorisationService(PersonneRepository personneRepository) {
            this.personneRepository = personneRepository;
        }

@Override
    public List<User> getAll(String where) {
        return personneRepository.getAll(where));
    }
    ...
    }

but when I run the test it seems no to mock the repo

This answer is for testng :

MockitoTestExecutionListener initializes mocks, and you need to use @MockBean annotation.

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(MockitoTestExecutionListener.class)
public class AutorisationServiceTest {   
    @MockBean
    PersonneRepository personneRepository;

Also, don't forget to add ResetMocksTestExecutionListener to prevent inter-test persistent mocks (see this issue ).

You should decide whether you want to write a Spring test or a Mockito test.

A spring test will load the full context and you can then work with @Autowired and @MockBean :

@RunWith(SpringJUnit4ClassRunner.class)
public class AutorisationServiceTest {
    
  @Autowired
  private IAutorisationService service;
    
  @MockBean
  PersonneRepository personneRepository;

A Mockito test runs much faster, but you will only have the objects you explicitly configured with @InjectMocks or @Mock :

@RunWith(MockitoJUnitRunner.class)
public class AutorisationServiceTest {
  
  @InjectMocks
  private IAutorisationService service;
    
  @Mock
  PersonneRepository personneRepository;

Note that you never have to instanciate the mock with Mockito.mock() when using annotations and specialized runners.

I guess your PersonneRepository is a @Service, @Component or @Repository. So you should use auto-wiring here as well:

@Service
public class AutorisationService implements IAutorisationService {

   @Autowired    
   private PersonneRepository personneRepository;

You can't use both @Mock and Mockito.mock . Choose one of them

In @Before setup method you need to add MockitoAnnotations.initMocks(this). This will inject your mocked services. I have modified the code below :

@RunWith(SpringJUnit4ClassRunner.class)
    public class AutorisationServiceTest {
    
        @Mock
        PersonneRepository personneRepository;

        @InjectMocks
        private AutorisationService service;
    
        @Before
        public void setup() {
          MockitoAnnotations.initMocks(this);
        }
    
    
        @Test
        public void should_Find_GardeWith2Affectations() throws IOException {
    
            when(personneRepository.getAll(anyString())).thenReturn(DataLoader.mockData());
            service.getAll("rules");
        }
    
    }

    @Service
    public class AutorisationService implements IAutorisationService {
    
        private final PersonneRepository personneRepository;
    
        public AutorisationService(PersonneRepository personneRepository) {
            this.personneRepository = personneRepository;
        }

@Override
    public List<User> getAll(String where) {
        return personneRepository.getAll(where));
    }
    ...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM