java - 具有 Spring Boot 集成的 Cucumber Java - Spring @Autowired 抛出 NullPointer 异常

标签 java spring unit-testing cucumber-junit cucumber-java

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

spring boot应用类,

@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);
    }
}

cucumber 单元类,

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

}

cucumber 定义类,

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);
    }
 }

与 cucumber 类的spring集成,

@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);
    }
}

这样定义class是不行的,抛出NullPointer异常,

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应用类进行cucumber单元测试的正确方式。请建议我解决此问题。

最佳答案

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

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

cucumber-spring 的 pom.xml,

<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>

修改后的 cucumber 定义文件,

@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 集成问题。我们将能够使用 Cucumber 指定的更改来测试 Spring Boot 应用程序。

关于java - 具有 Spring Boot 集成的 Cucumber Java - Spring @Autowired 抛出 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37859073/

相关文章:

java - Spring Integration Bridge 注解配置导致 BeanCurrentlyInCreationException

Angular 7 单元测试 Observable 订阅调用

javascript - Chrome扩展单元测试抛出 'unsafe-eval'与Sinon

java - invokeAll 的参数(集合)是否取决于队列大小?

java - Hibernate 发出个人查询而不是加入

java - Java 中的 Perlin 噪声问题

java - Spring-Java配置@Bean参数

unit-testing - 仅使用 Hunit 在 Haskell 中创建和运行最小测试套件

Java:创建一个图像文件并在其中写入字符串?

java - 错误 : Could not write JSON: Class java. util.ArrayList 不是映射的子类型 - Spring 应用程序中序列化为 JSON