Merge pull request #4611 from priyeshmashelkar/master
BAEL-1907 Mockito @Mock vs @MockBean vs Mockio.mock()
This commit is contained in:
commit
add380d17a
|
@ -0,0 +1,33 @@
|
|||
package org.baeldung;
|
||||
|
||||
import org.baeldung.repository.UserRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MockAnnotationTest {
|
||||
|
||||
@Mock
|
||||
UserRepository mockRepository;
|
||||
|
||||
@Test
|
||||
public void testMockAnnotation() {
|
||||
Mockito.when(mockRepository.count()).thenReturn(123L);
|
||||
long userCount = mockRepository.count();
|
||||
Assert.assertEquals(123L, userCount);
|
||||
Mockito.verify(mockRepository).count();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMockitoMockMethod() {
|
||||
UserRepository localMockRepository = Mockito.mock(UserRepository.class);
|
||||
Mockito.when(localMockRepository.count()).thenReturn(111L);
|
||||
long userCount = localMockRepository.count();
|
||||
Assert.assertEquals(111L, userCount);
|
||||
Mockito.verify(localMockRepository).count();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.baeldung;
|
||||
|
||||
import org.baeldung.repository.UserRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
public class MockBeanAnnotationIntegrationTest {
|
||||
|
||||
@MockBean
|
||||
UserRepository mockRepository;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void testMockBean() {
|
||||
Mockito.when(mockRepository.count()).thenReturn(123L);
|
||||
UserRepository userRepoFromContext = context.getBean(UserRepository.class);
|
||||
long userCount = userRepoFromContext.count();
|
||||
Assert.assertEquals(123L, userCount);
|
||||
Mockito.verify(mockRepository).count();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue