MockBean定义行为和验证
@SpringBootTest
class HelloControllerTest {
@MockBean
private Service service;
@Autowired
private HelloController helloController;
@Test
void test ( ) {
String s = helloController. testMock ( 3 ) ;
Assert . assertNull ( s) ;
verify ( service, times ( 1 ) ) . testMock ( 3 ) ;
}
@Test
void test0 ( ) {
when ( service. testMock ( 3 ) ) . thenReturn ( "mock" ) ;
String s = helloController. testMock ( 3 ) ;
Assert . assertEquals ( "mock" , s) ;
verify ( service, times ( 1 ) ) . testMock ( 3 ) ;
}
@Test
void test1 ( ) {
when ( service. testMock ( 3 ) ) . thenReturn ( "mock" ) ;
String s = helloController. testMock ( 3 ) ;
Assert . assertEquals ( "service error" , s) ;
verify ( service, times ( 1 ) ) . testMock ( 3 ) ;
}
}
@RestController
public class HelloController {
@Autowired
private Service service;
@RequestMapping ( "/test" )
public String testMock ( int i) {
String s;
try {
s = service. testMock ( i) ;
} catch ( Exception e) {
s = "service error" ;
}
return s;
}
}
@Service
public class HelloService {
private ConcurrentHashMap < String , String > DB = new ConcurrentHashMap < > ( ) ;
public String testMock ( int i) {
return "success" ;
}
}
MockBean调用真实方法
@Test
void test2 ( ) {
when ( service. testMock ( 3 ) ) . thenReturn ( "mock" ) ;
when ( service. testMock2 ( 3 ) ) . thenCallRealMethod ( ) ;
String s = helloController. testMock2 ( 3 ) ;
Assert . assertEquals ( "mock success2" , s) ;
verify ( service, times ( 1 ) ) . testMock ( 3 ) ;
}
@RequestMapping ( "/test2" )
public String testMock2 ( int i) {
String s;
String s2;
s = service. testMock ( i) ;
s2 = service. testMock2 ( i) ;
return s + " " + s2;
}
public String testMock ( int i) {
return "success" ;
}
public String testMock2 ( int i) {
return "success2" ;
}
验证时参数是对象时
@Test
void test3 ( ) {
when ( service. testMock3 ( any ( ) ) ) . thenReturn ( "mock" ) ;
String s = helloController. testMock3 ( "Joe" , 2 ) ;
Assert . assertEquals ( "mock" , s) ;
verify ( service, times ( 1 ) ) . testMock3 ( argThat ( ( People people) -> {
Assert . assertEquals ( "Joe" , people. getName ( ) ) ;
Assert . assertEquals ( new Integer ( 3 ) , people. getWorkYear ( ) ) ;
return true ;
} ) ) ;
}
@Builder
@Data
public class People {
private String name;
private Integer workYear;
}
@RequestMapping ( "/test3" )
public String testMock3 ( String name, Integer workYear) {
String s;
People people = People . builder ( ) . name ( name) . workYear ( workYear + 1 ) . build ( ) ;
s = service. testMock3 ( people) ;
return s;
}
public String testMock3 ( People people) {
return "success3" ;
}
spyBean定义行为和验证
@SpringBootTest
public class HelloController2Test {
@SpyBean
private Service service;
@Autowired
private HelloController helloController;
@Test
void test ( ) {
String s = helloController. testMock ( 3 ) ;
Assert . assertEquals ( "success" , s) ;
verify ( service, times ( 1 ) ) . testMock ( 3 ) ;
}
@Test
void test0 ( ) {
when ( service. testMock ( 3 ) ) . thenReturn ( "mock" ) ;
String s = helloController. testMock ( 3 ) ;
Assert . assertEquals ( "mock" , s) ;
verify ( service, times ( 1 ) ) . testMock ( 3 ) ;
}
}
验证需要等待时间
@SpringBootTest
public class HelloController2Test {
@SpyBean
private Service service;
@Autowired
private HelloController helloController;
@Test
void test4 ( ) {
helloController. testMock4 ( ) ;
Awaitility . await ( )
. atMost ( 2 , TimeUnit . SECONDS)
. untilAsserted ( ( ) -> {
String queryString = service. queryDB ( ) ;
Assert . assertEquals ( "success4" , queryString) ;
} ) ;
}
}
< dependency>
< groupId> org.awaitility</ groupId>
< artifactId> awaitility</ artifactId>
< version> 4.1.0</ version>
< scope> test</ scope>
</ dependency>
@RequestMapping ( "/test4" )
public void testMock4 ( ) {
new Thread ( ( ) -> {
service. updateDB ( ) ;
} ) . start ( ) ;
}
public class Service {
private ConcurrentHashMap < String , String > DB = new ConcurrentHashMap < > ( ) ;
public String queryDB ( ) {
return DB. get ( "1" ) ;
}
public void updateDB ( ) {
ThreadUtil . sleep ( 1 ) ;
DB. put ( "1" , "success4" ) ;
}
}
http请求MockMvc验证
@SpringBootTest
@AutoConfigureMockMvc
public class ShopControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testShopController ( ) throws Exception {
String correlationId = UUID. randomUUID ( ) . toString ( ) ;
AddGoodsRequestDTO requestDTO = AddGoodsRequestDTO . builder ( ) . goodsNumber ( 112323123 ) . version ( 5 ) . type ( 2 ) . name ( "phone" ) . build ( ) ;
HttpHeaders headers = new HttpHeaders ( ) ;
headers. setContentType ( MediaType . APPLICATION_JSON) ;
headers. add ( "Correlation-Id" , correlationId) ;
String response = "{\"returnCode\":\"10010\",\"returnMessage\":\"success\",\"data\":true}" ;
mockMvc. perform (
MockMvcRequestBuilders . post ( "/goods" )
. headers ( headers)
. content ( JSON. toJSONString ( requestDTO) ) )
. andExpect ( status ( ) . isOk ( ) )
. andExpect ( content ( ) . json ( response) ) ;
}
}
@RestController
public class ShopController {
@PostMapping ( "/goods" )
public ResponseResult < Object > addGoods ( @RequestBody AddGoodsRequestDTO requestDTO) {
return ResponseResult . builder ( ) . returnCode ( "10010" ) . returnMessage ( "success" ) . data ( Boolean . TRUE) . build ( ) ;
}
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AddGoodsRequestDTO {
private Integer goodsNumber;
private Integer version;
private Integer type;
private String name;
}
@Data
@Builder
public class ResponseResult < T > {
private String returnCode;
private String returnMessage;
private T data;
}