【发布时间】:2021-11-03 21:41:17
【问题描述】:
请提示我如何让 Sleuth Baggage 在集成测试中工作。 当测试通过模拟 HTTP 请求时它按预期工作,但在测试直接调用服务时不起作用。 我的应用是(Spring Boot 2.5.4,Spring Cloud 2020.0.3):
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
BaggageField myBaggageField() {
return BaggageField.create("my-field-name");
}
@RestController
static class MyController {
@AutowiredMyService myService;
@GetMapping
String callService() {
return myService.updateAndGet();
}
}
@Service
static class MyService {
@Autowired BaggageField baggageField;
public String updateAndGet() {
baggageField.updateValue("value");
return baggageField.getValue();
}
}
}
应用程序.yml:
spring.sleuth.baggage.remote-fields: my-field-name
测试:
@SpringBootTest
@AutoConfigureMockMvc
class MyApplicationTests {
@Autowired
MyService myService;
@Autowired
MockMvc mockMvc;
@Test
void testBaggageByService() {
assertThat(myService.updateAndGet()).isEqualTo("value"); // FAILS
}
@Test
void testBaggageByController() throws Exception {
mockMvc.perform(get("/")).andExpect(content().string("value")); // PASS
}
}
【问题讨论】:
标签: spring-boot-test spring-cloud-sleuth