tracer在spring云契约测试中出现空指针异常

zour9fqk  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(468)

因为tracer.currentspan()方法无法在SpringCloudContract测试环境中生成span对象,所以我得到了低于null的指针异常。我尝试了一些不同的方法,比如使用setter和@contextconfiguration的显式方法,tracerautoconfiguration,但它不起作用。有谁能解决这个问题或有类似的问题吗?下面是调试它所需的详细信息。
堆栈跟踪:

ERROR!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    at com.example.controller.ContractVerifierTest.validate_shouldReturnResponse(ContractVerifierTest.java:28)
Caused by: java.lang.NullPointerException
    at com.example.controller.ContractVerifierTest.validate_shouldReturnResponse(ContractVerifierTest.java:28)

控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import brave.Tracer;

@RestController
public class EvenOddController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    Tracer tracer;

    @PostMapping("/account/save")
    public Account save(@RequestBody Account account) {
        String transactionId = tracer.currentSpan().context().traceIdString();
        return account;
    }
}

基本测试类

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@DirtiesContext
@AutoConfigureMessageVerifier
@ExtendWith(SpringExtension.class)
public class BaseTestClass {

    @Autowired
    private EvenOddController evenOddController;

    @BeforeEach
    public void setup() {
        StandaloneMockMvcBuilder standaloneMockMvcBuilder = MockMvcBuilders.standaloneSetup(evenOddController);
        RestAssuredMockMvc.standaloneSetup(standaloneMockMvcBuilder);
    }
}
643ylb08

643ylb081#

这是因为使用mockmvc,您必须自己手动传递跟踪过滤器。请要么嘲笑 Tracer 或者提取对为了测试而重写的方法的跟踪程序调用

相关问题