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.
The spring boot application class,
@SpringBootApplication
public class SpringBootCucumberTest {
public static void main(String[] args) {
SpringApplication.run(SpringBootCucumberTest.class, args);
}
}
The class to be tested,
@Component
public class Library {
private final List<BookVo> store = new ArrayList<BookVo>();
public void addBook(final BookVo BookVo) {
this.store.add(BookVo);
}
}
The cucumber unit class,
@RunWith(Cucumber.class)
@CucumberOptions(format = "pretty", features = "src/test/resources")
public class BookSearchTest {
}
The cucumber definition class,
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);
}
}
The spring integration with cucumber class,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringBootCucumberTest.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest
public class AbstractDefinition {
public AbstractDefinition() {
}
}
It works if the definition class this way,
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);
}
}
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);
}
}
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.