小编use*_*047的帖子

带有Spring Boot Integration的Cucumber Java - Spring @Autowired会抛出NullPointer异常

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

春季启动应用程序类,

@SpringBootApplication
public class SpringBootCucumberTest {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootCucumberTest.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

要测试的课程,

@Component
public class Library {

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

    public void addBook(final BookVo BookVo) {
        this.store.add(BookVo);
    }
}
Run Code Online (Sandbox Code Playgroud)

黄瓜单位类,

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

}
Run Code Online (Sandbox Code Playgroud)

黄瓜定义类,

public class BookSearchSteps extends AbstractDefinition{

    @Autowired
    private Library library;

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

    @Given(".+book with the title '(.+)', written …
Run Code Online (Sandbox Code Playgroud)

java spring unit-testing cucumber-junit cucumber-java

4
推荐指数
1
解决办法
4570
查看次数

Spring - 需要在 @Component 类中自动装配 @RestController 类

我在同一个 maven 父模块下有 web 和核心项目的组合,

父级 - Web (com.parent.test.web) - 核心 (com.parent.test.core)

我想引用核心项目中的web模块依赖来调用web模块中的一些api

Web 项目示例,

com.test.parent.web

public interface RestInterface {
    public ResponseEntity load();
}

@RestController
public class RestInterfaceImpl implements RestInterface {

    @Override
    @RequestMapping(value = "/getData", method = RequestMethod.GET, produces = APPLICATION_JSON)
    public @ResponseBody ResponseEntity<Object> load() {

    }
}
Run Code Online (Sandbox Code Playgroud)

核心项目样本,

com.test.parent.core

@Component
public class CoreImpl implements CoreInterface {

    // Is this possible to autowire
    @Autowired
    private RestInterface restInterface;

    public boolean getOptions() {
        ResponseEntity<Object> results = restInterface.load();
        for (Object o : results) …
Run Code Online (Sandbox Code Playgroud)

java spring maven

2
推荐指数
1
解决办法
4423
查看次数