首页 > 解决方案 > 黄瓜 StepDef ResultActions NullPointerException

问题描述

.feature尝试执行将 JSON 从文件传递到本地主机上的 REST 端点的 Cucumber StepDefs 文件时,我收到以下 NullPointerException ;

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

标签: javaspring-bootcucumbermockmvc

解决方案


当您尝试取消引用为空的变量或字段时,会发生空指针异常。因此,如果您尝试调用performwhile mockMvcis mockMvcnull ,您将得到一个空指针异常。

如果您仔细阅读堆栈跟踪,您会发现这就是它试图告诉您的内容。

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


推荐阅读