forked from rbmonster/learning-note
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockitoTestDemo.java
More file actions
61 lines (53 loc) · 2.07 KB
/
Copy pathMockitoTestDemo.java
File metadata and controls
61 lines (53 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import com.four.unittest.UnitTestApplication;
import com.four.unittest.controller.UnitController;
import com.four.unittest.service.UnitInterface;
import com.four.unittest.service.UnitTestService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
@Slf4j
@ActiveProfiles(profiles = "test")
@SpringBootTest(classes = UnitTestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class MockitoTestDemo {
@Autowired
private UnitTestService unitTestService;
@MockBean
private UnitInterface unitInterface;
@Test
public void test1() {
UnitInterface spy = Mockito.spy(unitInterface);
Mockito.when(spy.register(Mockito.anyString())).thenReturn("gogogo");
String s = spy.register("123");
log.info(s);
}
@Test
public void test() {
UnitTestService spy = Mockito.spy(unitTestService);
Mockito.when(spy.testSql()).thenReturn("fadsfasdfasdf");
String result = spy.testSql();
log.info(result); // return fadsfasdfasdf
Mockito.when(spy.testParameter(Mockito.anyInt(),
Mockito.anyString(),
Mockito.any(),
Mockito.anyList(),
Mockito.anyMap()))
.thenReturn("mock success");
String res = spy.testParameter(1, "sddf", BigDecimal.ZERO, new ArrayList<>(), new HashMap<>());
log.info(res);
}
}