0

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

1 Answers1

0

That's because with MockMvc you'd have to manually pass the tracing filter yourself. Please either mock the Tracer or extract the tracer call to a method that you would override for the sake of tests

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32