I Am getting below Null Pointer Exception because tracer.currentSpan() method is not able to generate Span object in Spring Cloud Contract Test environment. I tried few different approaches like explicit approach using setter and @ContextConfiguration,TracerAutoConfiguration but its not working. can anyone resolve it or had similar issue? below are required details to debug it.
Stack Trace:
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)
Controller
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;
}
}
Base Test Class
@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);
}
}