Cucumber, Junit, and running from a Java Application not working

420 views
Skip to first unread message

cyas...@gmail.com

unread,
Mar 19, 2014, 12:09:12 PM3/19/14
to cu...@googlegroups.com
We have a bunch of Cucumber tests that we currently run through the IDE by selecting "Run As > JUnit Test". Doing it this way everything works and runs fine.

We however need to be able to kick these tests off from within a Java application. Looking here I found a couple ways to run Junit tests from within java such as:

JUnitCore junit = new JUnitCore();
Result result = junit.run(testClasses);

or
public static void main(String args[]) {
  org
.junit.runner.JUnitCore.main("junitfaq.SimpleTest");
}

Doing it this way however does not work. Our tests start to run and then we are shown "You can implement missing steps with the snippets below:"

I can not understand why when I manually kick off the tests to run as a Junit everything works, but when I do the same from within a Java application the step definition are not found. It finds the feature files but can't find the step methods to match them.

For testing purposes I have the main with the code to start the Junit test in the same .java file I select "Run As > JUnit Test" from.

Any thoughts? Thanks in advance!

cyas...@gmail.com

unread,
Mar 21, 2014, 1:09:22 PM3/21/14
to cu...@googlegroups.com
Getting farther now. I looked at the command line in debug mode with each type of run and it appears running it through eclipse's menu / maven2eclipse adds "test-classes" to the classpath, where as using JunitCore runner it does not. This is most likely my issue and now I am on figuring out how to either add this to the classpath or kick it off the same way eclipse does using m2eclipse. Again very new to this so forgive me if I am misunderstanding anything.

cyas...@gmail.com

unread,
Mar 21, 2014, 3:03:02 PM3/21/14
to cu...@googlegroups.com
For those coming here for answers I found it easiest just to move my step definition files into a "src/main/test/" folder where as they previously lived in a "src/test/java/" folder. They now execute fine either using Junit or from a main method which kicks off junit. 

Reading online it seemed that passing an additional classpath at runtime would not necesarily work because classpath would only be read in once at the start of execution and not updated with the additional folder.

I also saw how to run a maven test from the command line using something like "mvn test -Dtest" but it didn't seem possible to do this from java code aside from calling a .bat file.

In the end the way we solved it seemed to be the cleanest way to do it, though if anyone wants to chime in please do so!

More information about finding what eclipse is passing during runtime which led me to my solution: http://stackoverflow.com/questions/7892070/how-does-eclipse-actually-run-junit-tests
Reply all
Reply to author
Forward
0 new messages