@RunWith (Cucumber.class) and @Autowired MockMvc

I am trying to use MockMvc in Cucumber tests, but spring dependencies are not allowed.

I create this class:

@RunWith(Cucumber.class) @CucumberOptions(format = "pretty", features = "src/test/resources/features"}) @SpringBootTest public class CucumberTest { } 

to start the cucumber function

And this class for steps:

 @WebMvcTest(VersionController.class) @AutoConfigureWebMvc public class VersionControllerSteps { @Autowired private MockMvc mvc; private MvcResult result; @When("^the client calls /version$") public void the_client_issues_GET_version() throws Throwable { result = mvc.perform(get("/version")).andDo(print()).andReturn(); } @Then("^the client receives status code of (\\d+)$") public void the_client_receives_status_code_of(int statusCode) throws Throwable { assertThat(result.getResponse().getStatus()).isEqualTo(statusCode); } @And("^the client receives server version (.+)$") public void the_client_receives_server_version_body(String version) throws Throwable { assertThat(result.getResponse().getContentAsString()).isEqualTo(version); } } 

but this is a throw exception:

 java.lang.NullPointerException at com.example.rest.VersionControllerSteps.the_client_issues_GET_version(VersionControllerSteps.java:30) at ✽.When the client calls /version(version.feature:8) 

Here is the meaning of .feature:

 Feature: the version can be retrieved As a api user I want to know which api version is exposed In order to be a good api user Scenario: client makes call to GET /version When the client calls /version Then the client receives status code of 200 And the client receives server version 1.0 

How to configure my test to use cucumber and spring-boot?

Thanks in advance.

+5
source share
1 answer

From your list of codes and the error log, it is unclear whether the problem is installing cucumber + spring or just an application error.

The stack trace point on line 30, which throws a null pointer exception. You can see from your code list that this might be due to the instructions result.getResponse().getContentAsString() .

It might be worth checking to see if your controller really returns a body. For example, you might need to mark the return value with the @ResponseBody annotation

  @RequestMapping( value = "/campaigns/new", method = RequestMethod.GET, ) public @ResponseBody String vers() { return "1.0.1"; } 
0
source

Source: https://habr.com/ru/post/1265520/


All Articles