本文介绍了带有Spring Boot Integration的Cucumber Java - Spring @Autowired会抛出NullPointer异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为测试每个功能的Spring启动应用程序编写cucumber-java单元测试。当我与spring boot集成时,@ Autowired类会抛出NullPointer异常。

I am writing cucumber-java unit tests for a Spring boot application testing each functionalities. When i integrated with spring boot, the @Autowired classes throws NullPointer Exception.

Spring启动应用程序类

@SpringBootApplication
public class SpringBootCucumberTest {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCucumberTest.class, args);
    }
}

要测试的课程

@Component
public class Library {

    private final List<BookVo> store = new ArrayList<BookVo>();

    public void addBook(final BookVo BookVo) {
        this.store.add(BookVo);
    }
}

黄瓜单位类

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

}

黄瓜定义类

public class BookSearchSteps extends AbstractDefinition{

    @Autowired
    private Library library;

    private List<BookVo> result = new ArrayList<BookVo>();

    @Given(".+book with the title '(.+)', written by '(.+)', published in (.+)")
    public void addNewBook(final String title, final String author, @Format("dd MMMMM yyyy") final Date published) {
        BookVo book = new BookVo(title, author, published);
        library.addBook(book);
    }
 }

春季与黄瓜类的整合

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringBootCucumberTest.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest
public class AbstractDefinition {

    public AbstractDefinition() {
    }
}

如果定义类这样

public class BookSearchSteps extends AbstractDefinition{

    private Library library = new Library();

    private List<BookVo> result = new ArrayList<BookVo>();

    @Given(".+book with the title '(.+)', written by '(.+)', published in (.+)")
    public void addNewBook(final String title, final String author, @Format("dd MMMMM yyyy") final Date published) {
        BookVo book = new BookVo(title, author, published);
        library.addBook(book);
    }
}

如果定义类不起作用这样,抛出NullPointer异常,

It does not work if the definition class this way, throwing NullPointer exception,

public class BookSearchSteps extends AbstractDefinition{

    @Autowired
    private Library library;

    private List<BookVo> result = new ArrayList<BookVo>();

    @Given(".+book with the title '(.+)', written by '(.+)', published in (.+)")
    public void addNewBook(final String title, final String author, @Format("dd MMMMM yyyy") final Date published) {
        BookVo book = new BookVo(title, author, published);
        library.addBook(book);
    }
}

@Autowired在这个地方也不起作用我也是我运行测试时无法看到Spring启动应用程序日志。是集成springboot应用程序类进行黄瓜单元测试的正确方法。请建议我修复此问题。

The @Autowired does not work at this place also I could not see Spring boot application logs when I run the test. Is the correct way to integrated springboot application classes for cucumber unit testing. Please suggest me a fix for this.

推荐答案

以下解决了这个问题,需要在maven依赖项中包含cucumber-spring。 / p>

The following solved the issue, need to include cucumber-spring in maven dependencies.

<dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>

黄瓜春天的pom.xml,

The pom.xml for cucumber-spring,

<dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>

修改后的黄瓜定义文件,

The modified cucumber definition file,

@ContextConfiguration(classes = SpringBootCucumberTest.class, loader = SpringApplicationContextLoader.class)
public class BookSearchSteps {

    @Autowired
    private Library library;

    private List<BookVo> result = new ArrayList<BookVo>();

    @Given(".+book with the title '(.+)', written by '(.+)', published in (.+)")
    public void addNewBook(final String title, final String author, @Format("dd MMMMM yyyy") final Date published) {
        BookVo book = new BookVo(title, author, published);
        library.addBook(book);
    }
}

这解决了@Autowired和springboot集成问题。我们将能够使用黄瓜指定的更改来测试spring boot应用程序。

This solved the @Autowired and springboot integrations. We ll be able to test the spring boot applications with the changes specified using cucumber.

这篇关于带有Spring Boot Integration的Cucumber Java - Spring @Autowired会抛出NullPointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:18