Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 619
user3095047 Avatar asked Mar 12 '23 23:03

user3095047


1 Answers

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>

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

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

like image 120
user3095047 Avatar answered Apr 25 '23 11:04

user3095047