File tree Expand file tree Collapse file tree
spring-boot/src/test/java/org/baeldung Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .baeldung ;
2+
3+ import org .baeldung .repository .UserRepository ;
4+ import org .junit .Assert ;
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .mockito .Mock ;
8+ import org .mockito .Mockito ;
9+ import org .mockito .junit .MockitoJUnitRunner ;
10+
11+ @ RunWith (MockitoJUnitRunner .class )
12+ public class MockAnnotationTest {
13+
14+ @ Mock
15+ UserRepository mockRepository ;
16+
17+ @ Test
18+ public void testMockAnnotation () {
19+ Mockito .when (mockRepository .count ()).thenReturn (123L );
20+ long userCount = mockRepository .count ();
21+ Assert .assertEquals (123L , userCount );
22+ Mockito .verify (mockRepository ).count ();
23+ }
24+
25+ @ Test
26+ public void testMockitoMockMethod () {
27+ UserRepository localMockRepository = Mockito .mock (UserRepository .class );
28+ Mockito .when (localMockRepository .count ()).thenReturn (111L );
29+ long userCount = localMockRepository .count ();
30+ Assert .assertEquals (111L , userCount );
31+ Mockito .verify (localMockRepository ).count ();
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package org .baeldung ;
2+
3+ import org .baeldung .repository .UserRepository ;
4+ import org .junit .Assert ;
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .mockito .Mockito ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .boot .test .mock .mockito .MockBean ;
10+ import org .springframework .context .ApplicationContext ;
11+ import org .springframework .test .context .junit4 .SpringRunner ;
12+
13+
14+ @ RunWith (SpringRunner .class )
15+ public class MockBeanAnnotationIntegrationTest {
16+
17+ @ MockBean
18+ UserRepository mockRepository ;
19+
20+ @ Autowired
21+ ApplicationContext context ;
22+
23+ @ Test
24+ public void testMockBean () {
25+ Mockito .when (mockRepository .count ()).thenReturn (123L );
26+ UserRepository userRepoFromContext = context .getBean (UserRepository .class );
27+ long userCount = userRepoFromContext .count ();
28+ Assert .assertEquals (123L , userCount );
29+ Mockito .verify (mockRepository ).count ();
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments