Running cucumber test file from application button - java

I've set up a test file in src/main/java with my cucumber annotations containing class A, as well as a test file extending class A in src/test/java with the following annotation on class B:
#ContextConfiguration(locations = {"classpath:META-INF/application-config.xml", "classpath:META-INF/overrule.xml" })
This is working fine when I do a maven clean install.
What I would like to achieve though is being able to run a feature file through the cucumber setup of class A and see its output. So far I've managed to find a method which should allow me to run the cucumber test, but I can't seem to figure out what its arguments should be. Could anyone provide me with an example of how to implement the function cucumber.api.cli.Main.run()?
#Override
public void buttonClick(final ClickEvent event) {
try {
final String[] arguments = {"foo", "bar" };
cucumber.api.cli.Main.run(arguments, ClassLoader.getSystemClassLoader());
} catch (final Throwable e) {
e.printStackTrace();
}
}

I would invoke the command line version using cucumber.api.cli.Main.main(args);
where args is a String array with the parameters set. I would not use the run command you refer to.
The documentation describes all available options.
Another source may be the getting started project supplied by the Cucumber team: https://github.com/cucumber/cucumber-java-skeleton
It may be of specific interest to look into the Ant build script https://github.com/cucumber/cucumber-java-skeleton/blob/master/build.xml to see what arguments they supply to Cucumber.

Related

NetBeans : JUnit no tests executed

So I'm getting this persistent error using netbeans. I've got a LinkedList class which I am testing via a JUnit test, which I created by clicking on LinkedList.java: Tools -> Create/Update Tests and this LinkedListTest.java class is now located in test packages.
My LinkedList.java file works correctly when tested in a file with a main method.
public class LinkedListTest {
#Test
public void testAddFirst() {
LinkedList linkedList = new LinkedList();
Country c1 = new Country("Australia");
linkedList.addFirst(c1);
assertEquals("Australias", linkedList.getValue(0)); // Should fail a test
} // default test methods beneath
All my imports check out. JUnit 5.3.1 and I had to download apiguardian1.1.0.jar from MVN repository to clear an error for:
reason: class file for org.apiguardian.api.API$Status not found
I right-click in this file and select Test File, or use Ctrl+F6, I've selected Test File from the original LinkedList file, I've even used Alt+F6 which tests the whole project. Yet I'm met with 'No tests executed.', an empty Test Results window, and no Notifications. What am I doing wrong?
Thanks for any help
Edit: I just switched from netbeans to eclipse.
You forget to extend Runner with class --
use like below with class -
public class LinkedListTest extends Runner {
}
Hope this help you.

Does Maven offer hooks at the very beginning of runtime

Say we are running mvn test.
I am wondering if there is a way to configure Maven to run some files before executing tests. In my case, I want to configure a library, but don't want to have to configure this library for every entrypoint in my app/tests. I am just looking to configure the lib for every mvn lifecycle hook which invokes a runtime.
Something like this:
#MavenRuntimeLifecycle
public class Whatever {
public void runtimeBegin(){
// right when the java process starts up
Mylib.configure("foo");
}
public void runtimeEnd(){
// right before the process shuts down
}
}
I assume this would be a Maven specific thing - not that it has to be in the same Java process as my server or tests etc.
Note that using Node.js, I would simply do it like so:
export class MyLib {
isConfigLoaded = false;
static loadConfig(){
// ...
}
static void run(){
if(!this.isConfigLoaded){
MyLib.loadConfig(require('../some/path/to/.mylib.config.js'));
this.isConfigLoaded = true;
}
this.doTheThing();
}
}
I could do the same thing with Java or Maven project, and just store a .java file in the resources directory. It's more manual, but it could be done.

How to invoke the Cucumber runner class from a different main method

I'm new to using Command Line Interface. So I just have a question on how to invoke the runner class of the cucumber using CLI technique.
I have a Java program which contains a main method. When testers pass the argument which is test case, it will fetch the feature file. The java program invoke a custom made API which will fetch the correct feature file.
Next I'll have to invoke the Cucumber runner class to execute the test case. I need to pass this particular feature file as the argument. Two questions, Can we invoke the runner class from a different main method. I did some research and I was not able to find a concrete answer.
Two questions,
cucumber.api.cli.Main.main(arguments); So how do i specify the jar location of my runner class.
`FeatureFileCreation.main("xxxxx"); - API that fetches the right feature file
String[] arguments = {"-", ""};
cucumber.api.cli.Main.main(arguments);
How do I specify where my jar is located? How can I pass my feature file?`
Should I create a main method in the runner class, something like this? For the sake of using CLI,Since I need to create a runnable jar. I should have a main method in my runner class.
`
#RunWith(Cucumber.class)
#Cucumber.Options(features="C:/Users/IBM_ADMIN/Desktop/CRAutomation/CR Regression1/src/My.feature",glue={"bell.canada.step.definition"})
public class AutomationRunnerAction {
public void main(){
}
}`
Please note that, Getting the right feature file is 1 java API. I will invoking that API from one main method of one java program. The runner class with step definition and methods are a diff java program.
Unfortunately the accept answer is not correct. If you look at the source of Main.main() you'll notice that it contains: System.exit(exitstatus) which terminates the system.
The proper way to run the commandline programatically would be to use Main.run() like this:
String [] argv = new String[]{ "-g","","./src/main/java/featureDetails/Testing.feature"};
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
byte exitstatus = Main.run(argv, contextClassLoader);
Try this if this works. You do not need any Runner class. Just call the static main method of Main class that corresponds to running cucumber from cli.
public static void main(String[] args) throws Throwable {
//Your code to get feature file full path
Main.main(new String[]{"-g", "classpath to step definition file", "Full path to feature file"});
// My stepdefinition is inside java package at cucumber.sample.test
// My feature file is inside src/test/resources/features/samplethree.feature
}
For additional parameters like tags or plugin use "-t","#Tags". Important the feature file path has to be the last option.
I am running this for Eclipse with Maven setting up classpath and jar dependencies.

Cucumber JVM - Report not yet generated when attempting to send via email using #AfterClass

I am attempting to gather up all the files my tests generate (log file, screenshots, cucumber report etc) and send them via email. I'm doing this from the Runner class using JUnit's #AfterClass annotation.
#RunWith(Cucumber.class)
#CucumberOptions(
features = "src/test/resources/Features"
, glue = { "stepDefinition" }
, tags = { "#Teeest" }
, monochrome = true
, strict = false
, plugin = { /* "pretty", */ "html:target/cucumber-html-report","json:target/cucumber-html-report/cucumber-json-" + "report.json" })
public class TestRunner {
#AfterClass
public static void sendReport() {
SomeClass.sendMail();
}
}
Everything works fine except for the cucumber reports (both html and json), which are blank. When i manually check it, it looks good so I'm assuming it's generated sometime after this method is executed.
Does anyone have an idea of how I can get around this issue?
I'm thinking of either getting the Cucumber Reports plugin for Jenkins, writing a shell script to execute via Maven POM or a separate Java app that looks for the files, zips and sends the email.
All 3 of these ideas come with drawbacks so I'd really love another hook-type approach to this, if possible, as file locations have dynamic names and would be a lot easier to get the actual locations within the test suite than to look for them afterward.
Thanks!
PS: I am junior at this stuff so please don't hold back on details :)

Showing JUnit view in Eclipse when running tests from Code

When I run just my Testclass in Eclipse I get the JUnit view showing the tree structure and if the test was successful. If I start my Test from code:
JUnitCore core = new JUnitCore();
core.run(SimpleTests.class);
the view does not show. Can I change this?
On your toolbar click Windows-->Show View-->Others.
Type "Junit" without quotes. Select from list and click OK.
I suppose you run your code in the main method like this example :
public class MiniTest extends TestCase
{
#Test
public void test()
{
System.out.println("Running test");
}
public static void main(String[] args)
{
JUnitCore core = new JUnitCore();
core.run(MiniTest.class);
}
In this case, it will not show your view because you launch your program as a java application (alt-shift-X-J). You will see "Running test" in the console but that is.
If you want to see the Junit View you must launch your program as a Junit test (alt-shit-X-T).
Note that with this manner the main() is not necessary, Eclipse will directly launch the method tagged with #Test. You can also group tests with #Suite.
You can't interact with the JUnit view from your code using JUnitCore.
You can however, add your tests to the JUnitView if you use a #Parameterized test, or if you implement your own test runner. Try extending the Suite class and reimplement one of the constructors with the tests that you want to execute.
I get this problem from time to time.
There is something corrupted in the eclipse workspace.
The only solution for me is to create a new workspace and bring in the projects.
It's a serious pain when there are fifty projects in the workspace.
If the view doesn't show, go to Window -> Perspective -> Reset Perspective..

Categories

Resources