简体   繁体   English

使用restTemplate进行集成测试

[英]Integration test sleuth with restTemplate

I have a running code with spring boot and spring cloud using sleuth to log the TraceId. 我有一个使用sleuth记录TraceId的spring boot和spring cloud运行代码。 However i have problems in call one controller from other in the integrationTest 但是我在IntegrationTest中从另一个调用一个控制器时遇到问题

  public class TraceIdApp {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(TraceIdApp.class, args);
    }

    }

    @Slf4j
    @Getter
    @RestController
    public class TraceIdController {

    private SpanAccessor spanAccessor;
    private RestTemplate restTemplate;
    private String url;

    @Autowired
    public TraceIdController(SpanAccessor spanAccessor, RestTemplate restTemplate,
            @Value("http://localhost:${local.server.port:${server.port:8080}}/other") String url) {
        this.spanAccessor = spanAccessor;
        this.restTemplate = restTemplate;
        this.url = url;
    }

    @GetMapping(path = "/")
    public String handlerOne() {
        long traceId = spanAccessor.getCurrentSpan().getTraceId();
        log.info("loggin the real traceId => " + traceId);
        String response = getRestTemplate().getForObject(getUrl(), String.class);

        return "one" + response;
    }

    @GetMapping(path = "/other")
    public String handlerOther() {
        long traceId = spanAccessor.getCurrentSpan().getTraceId();
        log.info("loggin the real traceId => " + traceId);
        return "other";
    }
}

@Getter
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class TraceIdAppTest {

    @Autowired
    private SpanAccessor spanAccessor;

    @Autowired
    private MockMvc mockMvc;
    @Value("http://localhost:${local.server.port:${server.port:8080}}/other")
    String url;

    @Test
    public void test() throws Exception {
        mockMvc.perform(get("/")).andExpect(status().isOk());
        assertTrue("trace is wrong type", getSpanAccessor() instanceof DefaultTracer);
    }
}

running on test the first i notice its that the port on url take a correct value on the test, however its value is zero on the controller. 首先在测试上运行,我注意到它在url上的端口在测试中取正确的值,但是在控制器上它的值为零。 hardcoding the port didnt fix the problem. 对端口进行硬编码并不能解决问题。

I don't think it has anything to do with Sleuth. 我认为这与Sleuth无关。 You're using @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) . 您正在使用@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)。 So the port will be set at some point to the bound port. 因此,该端口将在某个位置设置为绑定端口。 Initially it will be equal to 0, that's why you get the port with value equal to 0 injected to your controller. 最初它等于0,这就是为什么将值等于0的端口注入控制器的原因。 You can try to inject the port via a setter at a later time (sth like this https://github.com/spring-cloud/spring-cloud-contract/blob/master/samples/standalone/pact/pact-http-client/src/test/java/com/example/loan/LoanApplicationServiceTests.java#L30-L36 ). 您可以稍后尝试通过设置器注入端口(例如https://github.com/spring-cloud/spring-cloud-contract/blob/master/samples/standalone/pact/pact-http-客户端/src/test/java/com/example/loan/LoanApplicationServiceTests.java#L30-L36 )。 In your case you should resolve the value of the port once you're sending the request or when the test is ready to run. 对于您的情况,一旦发送请求或准备好运行测试,就应该解析端口的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM