java - 黄瓜 StepDef ResultActions NullPointerException
问题描述
.feature
尝试执行将 JSON 从文件传递到本地主机上的 REST 端点的 Cucumber StepDefs 文件时,我收到以下 NullPointerException ;
我试图以
ResultActions
其他方式实例化,收到相同的错误。与测试链接的控制器工作正常,并且指向正确的 REST 端点。
问题
result
在于personStepDefs
- 我不认为我缺少一个参数,
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
解决方案
当您尝试取消引用为空的变量或字段时,会发生空指针异常。因此,如果您尝试调用perform
while mockMvc
is mockMvc
null ,您将得到一个空指针异常。
如果您仔细阅读堆栈跟踪,您会发现这就是它试图告诉您的内容。
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
。
推荐阅读
- java - RestHighLevelClient 类型的方法 admin() 未定义
- kmalloc - 我们什么时候使用vmalloc和kmalloc?
- javascript - 如何更改子组件背景颜色
- ios - 如何使用 OpenGL ES 在 iOS SDK 中绘制黑色边缘?
- c# - 正确定位多个框架时如何使用条件符号(VS2017)
- sql - 有没有办法在同一个 laravel 查询中选择和聚合?
- php - 剪切字符串并删除最后一个逗号/空格后的所有内容
- sql - 如何在 PostgreSQL 中实现 CASE 返回多行
- youtube-api - 没有返回行
- amazon-web-services - 无法连接到 AWS EC2 实例