黄瓜 StepDef ResultActions NullPointerException
Cucumber StepDef ResultActions NullPointerException
我在尝试执行将 JSON 从 .feature
文件传递到本地主机上的 REST 端点的 Cucumber StepDefs 文件时收到以下 NullPointerException;
我尝试以其他方式实例化 ResultActions
,收到同样的错误。
连接到测试的控制器工作正常,并且指向正确的 REST 端点。
问题出在 personStepDefs
中的 result
- 我不认为我缺少
ResultActions result
的参数,因为我已经构建了 RequestBuilder
java.lang.NullPointerException at com.///.v2.PersonStepDefs.i\_add\_a\_new\_Person\_using\_POST\_at\_with\_JSON([PersonStepDefs.java:49](https://PersonStepDefs.java:49)) at
✽.I add a new Person using POST at "[http://localhost:8080/services/person/add](http://localhost:8080/services/person/add)" with JSON:(file:///C:/path/to/src/test/resources/Person.feature:6)
PersonStepDefs.java
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
/**
* Person Step Definition class to execute Scenario contained in Person.feature
* @author Lewis Jones
*
*/
public class PersonStepDefs {
@Autowired
private volatile WebApplicationContext wac;
@Autowired
private volatile MockMvc mockMvc;
private ResultActions result;
/**
* Runs the application server before every scenario.
*/
@BeforeClass
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@When("I add a new Person using POST at {string} with JSON:")
public void i_add_a_new_Person_using_POST_at_with_JSON(String request, String json) throws Exception {
result = mockMvc.perform(post(request).contentType(MediaType.APPLICATION_JSON)
.content(json.getBytes()));
}
@Then("the response code should be {int}")
public void the_response_code_should_be(Integer responseCode) throws Exception {
result.andExpect(status().is(responseCode));
}
}
RunMvcTest.java
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty","html:build/cucumber-html-report"},
features = "src/test/resources", strict = true)
@WebAppConfiguration
@ContextConfiguration(classes = V2Application.class)
/**
* A class to run the Cucumber .feature files located in 'features'
* @author Lewis Jones
*
*/
public class RunMvcTest {
}
Person.feature
Feature: Person CRUD
As a User, I want to add a Person
@repo
Scenario: Person.Repo.Add
When I add a new Person using POST at "http://localhost:8080/services/person/add" with JSON:
"""
{"firstName":"Lewis","lastName":"Jones","addressId":"1", "dob":"1999-07-11"}
"""
Then the response code should be 200
当您尝试取消引用一个为空的变量或字段时,会发生空指针异常。因此,如果您尝试在 mockMvc
上调用 perform
而 mockMvc
为空,您将得到一个空指针异常。
如果您仔细阅读堆栈跟踪,您会发现这是它试图告诉您的内容。
result = mockMvc.perform(....);
那么 mockMcv
怎么可能为空呢?你用 setup
方法初始化它对吗?这意味着 setup
没有被调用。您可以通过在方法中放置一个断点并调试您的测试来确认这一点。
import org.junit.BeforeClass;
....
/**
* Runs the application server before every scenario.
*/
@BeforeClass
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
并且 setup
不会被调用,因为 BeforeClass
是一个 JUnit 注释。黄瓜使用 io.cucumber.java.Before
.
我在尝试执行将 JSON 从 .feature
文件传递到本地主机上的 REST 端点的 Cucumber StepDefs 文件时收到以下 NullPointerException;
我尝试以其他方式实例化
ResultActions
,收到同样的错误。连接到测试的控制器工作正常,并且指向正确的 REST 端点。
问题出在
personStepDefs
中的 - 我不认为我缺少
ResultActions result
的参数,因为我已经构建了RequestBuilder
result
java.lang.NullPointerException at com.///.v2.PersonStepDefs.i\_add\_a\_new\_Person\_using\_POST\_at\_with\_JSON([PersonStepDefs.java:49](https://PersonStepDefs.java:49)) at
✽.I add a new Person using POST at "[http://localhost:8080/services/person/add](http://localhost:8080/services/person/add)" with JSON:(file:///C:/path/to/src/test/resources/Person.feature:6)
PersonStepDefs.java
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Transactional
/**
* Person Step Definition class to execute Scenario contained in Person.feature
* @author Lewis Jones
*
*/
public class PersonStepDefs {
@Autowired
private volatile WebApplicationContext wac;
@Autowired
private volatile MockMvc mockMvc;
private ResultActions result;
/**
* Runs the application server before every scenario.
*/
@BeforeClass
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@When("I add a new Person using POST at {string} with JSON:")
public void i_add_a_new_Person_using_POST_at_with_JSON(String request, String json) throws Exception {
result = mockMvc.perform(post(request).contentType(MediaType.APPLICATION_JSON)
.content(json.getBytes()));
}
@Then("the response code should be {int}")
public void the_response_code_should_be(Integer responseCode) throws Exception {
result.andExpect(status().is(responseCode));
}
}
RunMvcTest.java
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty","html:build/cucumber-html-report"},
features = "src/test/resources", strict = true)
@WebAppConfiguration
@ContextConfiguration(classes = V2Application.class)
/**
* A class to run the Cucumber .feature files located in 'features'
* @author Lewis Jones
*
*/
public class RunMvcTest {
}
Person.feature
Feature: Person CRUD
As a User, I want to add a Person
@repo
Scenario: Person.Repo.Add
When I add a new Person using POST at "http://localhost:8080/services/person/add" with JSON:
"""
{"firstName":"Lewis","lastName":"Jones","addressId":"1", "dob":"1999-07-11"}
"""
Then the response code should be 200
当您尝试取消引用一个为空的变量或字段时,会发生空指针异常。因此,如果您尝试在 mockMvc
上调用 perform
而 mockMvc
为空,您将得到一个空指针异常。
如果您仔细阅读堆栈跟踪,您会发现这是它试图告诉您的内容。
result = mockMvc.perform(....);
那么 mockMcv
怎么可能为空呢?你用 setup
方法初始化它对吗?这意味着 setup
没有被调用。您可以通过在方法中放置一个断点并调试您的测试来确认这一点。
import org.junit.BeforeClass;
....
/**
* Runs the application server before every scenario.
*/
@BeforeClass
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
并且 setup
不会被调用,因为 BeforeClass
是一个 JUnit 注释。黄瓜使用 io.cucumber.java.Before
.