My goal is running Cucumber scenarios while using Spring for objects creation and wiring. As far as I understood (from some Cucumber books), the Cucumber runner should be responsible for loading the application context as long as the path configured properly.
For example, if I have a runner Java class that looks like this:
@RunWith(Cucumber.class)
@CucumberOptions(features = {"classpath:xxx_features"},
glue = {"com.package.name"})
public class FunctionalDevIT {
}
then Cucumber runner will automatically scan and initialize all the beans that it will find so I won't have to load the application context manually as well as get beans one by one from the context.
Currently, just to get my head around the concept, I have a created the most basic configuration that consists of three Java classes:
Beans Class - the class that contains the beans
@Component public class BeansClass { @Bean public void test1() { System.out.println("test1!"); } }
Main class - the class that uses the beans defined in the Beans Class
@ContextConfiguration("classpath:/cucumber.xml") public class MainClass { @Autowired private BeansClass testclass; @Before public void navigate() { testclass.test1(); } }
At the moment that approach doesn't work for me and I get the NullPointerException
when I try to call the testclass.test1()
method in the Main Class which means that the context wasn't loaded after all.
Am I missing some configuration or perhaps my whole understanding of how that is supposed to be working is wrong?
PS All my files are currently in the same package.
I resolved this issue by adding and additional configuration class that has been annotated with @ComponentScan
and @Configuration
, so something like this:
@ComponentScan
@Configuration
public class ConfigurationClass {
. . .
}
Then I used this class in ConfigurationClass
in the main class:
@ContextConfiguration(classes = WebDriverConfiguration.class)
public class MainClass {
. . .
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.