繁体   English   中英

Cucumber Java 与 Spring Boot 集成 - Spring @Autowired 抛出 NullPointer 异常

[英]Cucumber Java with Spring Boot Integration - Spring @Autowired throws NullPointer Exception

我正在为测试每个功能的 Spring 启动应用程序编写黄瓜 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);
    }
}

黄瓜单元类

@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 异常,

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 boot 应用程序日志。 是为黄瓜单元测试集成 springboot 应用程序类的正确方法。 请建议我解决这个问题。

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

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

黄瓜弹簧的 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>

修改后的黄瓜定义文件,

@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 应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM