user3257736 user3257736 - 4 months ago 11
Java Question

Testing a Spring Boot application?

I want to create a Spring Boot test for a Controller-class.

The method I want to test is:

private String statusQueryToken;

@RequestMapping("/onCompletion")
public String whenSigningComplete(@RequestParam("status_query_token") String token){
this.statusQueryToken = token;


I am unsure about how to test things in Spring Boot.

If I wanted to test that the field
statusQueryToken
has been initialized with the
@RequestParam("status_query_token")
, how would I go about doing this?

Thank you!

Answer

There are several way you can approach this. My preferred way to test with the real tomcat instance.

    @RunWith(SpringJUnit4ClassRunner.class)
    @WebIntegrationTest("server.port:0")
    @SpringApplicationConfiguration(YourMainApplication.class)
    public class TestClass() {
        @Value("${local.server.port}")
        private int port;
    @Autowired
    private RestTemplate restTemplate;

   public <T> T get(String path, Class<T> responseType, Object... pathVariables) {
        return restTemplate.getForEntity(path, responseType, pathVariables).getBody();
    }

    }