Java 集成测试中Spring控制器中的模拟假客户端
我有一个网关控制器,在内部使用不同的服务。我试图为控制器编写一个模拟假客户机的集成测试,但它没有像我预期的那样工作 我有以下外国客户:Java 集成测试中Spring控制器中的模拟假客户端,java,unit-testing,spring-boot,mocking,feign,Java,Unit Testing,Spring Boot,Mocking,Feign,我有一个网关控制器,在内部使用不同的服务。我试图为控制器编写一个模拟假客户机的集成测试,但它没有像我预期的那样工作 我有以下外国客户: public interface StoreManagementClient { @RequestLine("GET /v1/stores/{storeId}") @Headers({"Accept: application/json", "Content-Type: application/json;charset=UTF-8"}) S
public interface StoreManagementClient {
@RequestLine("GET /v1/stores/{storeId}")
@Headers({"Accept: application/json", "Content-Type: application/json;charset=UTF-8"})
StoreDetails getStoreDetails(@Param("storeId") String storeId);
}
商店管理员:
@Validated
@Controller
@RequestMapping("${gateway.path}")
public class StoreController {
@Autowired
private StoreManagementClient storeManagementClient;
@GetMapping(value = "/stores/{storeId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<StoreDetails> getStoreDetails(
@PathVariable("storeId") String storeId) {
StoreDetails details = storeManagementClient.getStoreDetails(storeId);
return ResponseEntity.ok(details);
}
}
@已验证
@控制器
@RequestMapping(“${gateway.path}”)
公共类存储控制器{
@自动连线
私人店面管理客户店面管理客户;
@GetMapping(value=“/stores/{storeId}”,products=MediaType.APPLICATION\u JSON\u value)
公众响应getStoreDetails(
@路径变量(“存储ID”)字符串(存储ID){
StoreDetails=storeManagementClient.getStoreDetails(storeId);
返回响应正确(详细信息);
}
}
以及集成测试:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {GatewayServiceApplication.class},
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ClientIntegrationTest {
@Autowired
private StoreController storeController;
@MockBean
private StoreManagementClient storeManagementClient;
private MockClient mockClient;
@Before
public void setUp() throws Exception {
mockClient = new MockClient();
}
@Test
public void testCorrectGetStoreDetailsRequest() throws JsonProcessingException {
String storeId = "store-1";
StoreDetails storeDetails = new StoreDetails();
storeDetails.setId(storeId);
storeDetails.setType("grocery");
String response = new ObjectMapper().writeValueAsString(storeDetails);
storeManagementClient = Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.client(mockClient
.ok(RequestKey.builder(feign.mock.HttpMethod.GET, "/v1/stores/" + sroreId)
.headers(ImmutableMap.of(
ACCEPT, newArrayList("application/json"),
CONTENT_TYPE, newArrayList("application/json;charset=UTF-8"))).build(),
response
))
.target(new MockTarget<>(StoreManagementClient.class));
// when
ResponseEntity<StoreDetails> result = storeController.getStoreDetails(storeId);
// then
StoreDetails resultBody = result.getBody();
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(resultBody.getId()).isEqualTo(storeId);
assertThat(resultBody.getType()).isEqualTo("grocery");
}
@RunWith(SpringRunner.class)
@SpringBootTest(类={GatewayServiceApplication.class},
webEnvironment=SpringBootTest.webEnvironment.DEFINED\u端口)
公共类客户端集成测试{
@自动连线
私有存储控制器存储控制器;
@蚕豆
私人店面管理客户店面管理客户;
私有MockClient-MockClient;
@以前
public void setUp()引发异常{
mockClient=新的mockClient();
}
@试验
public void testCorrectGetStoreDetailsRequest()引发JsonProcessingException{
字符串storeId=“store-1”;
StoreDetails StoreDetails=新的StoreDetails();
storeDetails.setId(storeId);
storeDetails.setType(“杂货店”);
String response=new ObjectMapper().writeValueAsString(storeDetails);
storeManagementClient=Feign.builder()
.编码器(新的JacksonEncoder())
.decoder(新的JacksonDecoder())
.client(mockClient)
.ok(RequestKey.builder(feign.mock.HttpMethod.GET,“/v1/stores/”+sroreId)
.headers(不可变的map.of(
接受,newArrayList(“应用程序/json”),
CONTENT_TYPE,newArrayList(“application/json;charset=UTF-8”)).build(),
响应
))
.target(新的MockTarget(StoreManagementClient.class));
//什么时候
ResponseEntity结果=storeController.getStoreDetails(storeId);
//然后
StoreDetails resultBody=result.getBody();
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(resultBody.getId()).isEqualTo(storeId);
断言(resultBody.getType()).isEqualTo(“杂货店”);
}
我假设测试应该根据所描述的假客户端模拟响应,但实际上它返回null
我是否应该在模仿外国客户机时出错?可能,我混合了一个测试外国客户机和我自己的控制器,我需要将其分离,并为外国客户机编写单元测试,就像?
如果您能给我任何建议,我将不胜感激。首先,您可以用mock替换外国客户StoreManagementClient:
@MockBean
私人店面管理客户店面管理客户;
然后在测试中,您丢失了对模拟的引用,并指向本地对象:
storeManagementClient=Feign.builder()…build();
但控制器仍然使用模拟
在纯java中,您可以执行以下操作:
客户端=新客户端(空);
控制器=新控制器(客户端);
客户=新客户(1);
assertThat(controller.get()).isEqualTo(1))//否:等于null
另外,我希望将来我的回答会是建设性的首先,你用模拟来取代外国客户StoreManagementClient:
@MockBean
私人店面管理客户店面管理客户;
然后在测试中,您丢失了对模拟的引用,并指向本地对象:
storeManagementClient=Feign.builder()…build();
但控制器仍然使用模拟
在纯java中,您可以执行以下操作:
客户端=新客户端(空);
控制器=新控制器(客户端);
客户=新客户(1);
assertThat(controller.get()).isEqualTo(1))//否:等于null
我希望将来我的回答是建设性的