File tree Expand file tree Collapse file tree 5 files changed +57
-0
lines changed
javaCodeTest/src/test/java
main/java/com/zsx/mockito Expand file tree Collapse file tree 5 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -411,4 +411,10 @@ void t24() throws InterruptedException {
411411
412412 TimeUnit .SECONDS .sleep (5 );
413413 }
414+
415+ @ Test
416+ void t25 () {
417+
418+
419+ }
414420}
Original file line number Diff line number Diff line change 5959 <version >3.1.8</version >
6060 </dependency >
6161
62+ <dependency >
63+ <groupId >org.mockito</groupId >
64+ <artifactId >mockito-core</artifactId >
65+ <version >4.11.0</version >
66+ <scope >test</scope >
67+ </dependency >
68+
6269 </dependencies >
6370</project >
Original file line number Diff line number Diff line change 1+ package com .zsx .mockito ;
2+
3+ public interface UserService {
4+
5+ String getUserName (String userId );
6+ }
Original file line number Diff line number Diff line change 1+ package com .zsx .mockito ;
2+
3+ public class UserServiceImpl implements UserService {
4+
5+
6+ public String getUserName (String userId ) {
7+ return "zsx" ;
8+ }
9+
10+ }
Original file line number Diff line number Diff line change 1+ import com .zsx .mockito .UserService ;
2+ import com .zsx .mockito .UserServiceImpl ;
3+ import org .junit .jupiter .api .Test ;
4+ import org .mockito .Mockito ;
5+
6+ import static org .mockito .Mockito .*;
7+
8+ public class MockitoTest {
9+
10+
11+ @ Test
12+ void test01 () {
13+
14+ // 创建mock对象
15+ UserService userService = mock (UserService .class );
16+
17+ // 定义预期行为
18+ when (userService .getUserName ("1" )).thenReturn ("zsx" );
19+
20+ String userName = userService .getUserName ("1" );
21+
22+ verify (userService ).getUserName ("1" );
23+ //verify(userService, Mockito.times(10)).getUserName("1");
24+ }
25+
26+
27+ // todo zsx 待验证和补充
28+ }
You can’t perform that action at this time.
0 commit comments