Spring Controller中的Mock Feign客户端进行集成测试

时间:2019-05-29 10:12:48

标签: java unit-testing spring-boot mocking feign

我有一个使用内部不同服务的网关控制器。我尝试编写用于控制器的模拟假客户端的集成测试,但是它没有按我预期的那样工作。

我有以下Feign客户:

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);
    }
}

以及集成测试:

@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");
}

我认为测试应该根据描述的Feign Client模拟响应。但实际上它返回null

我应该如何嘲笑Feign客户程序?可能我混用了一个测试Feign客户端和我自己的控制器的测试,我需要将其分开并为Mock Feign Client example这样的Feign客户端编写单元测试? 任何建议,我将不胜感激

1 个答案:

答案 0 :(得分:0)

首先,将模拟客户端StoreManagementClient替换为模拟

@MockBean
private StoreManagementClient storeManagementClient;

然后在测试中,您失去了对模拟的引用并指向本地对象:

storeManagementClient = Feign.builder()....build();

但是控制器仍然使用模拟

在Plain Java中,您可以执行以下操作:

Client client = new Client(null);
Controller controller = new Controller(client);
client = new Client(1);
assertThat(controller.get()).isEqualTo(1)) // no no no: is equal to null

PS。我希望将来我的答案会很有建设性