Running a JUnit test from Groovy Console - java

How can I use the Groovy Console to kick off junit tests?
(Currently using Groovy 1.6.0)

I currently use:
junit.textui.TestRunner.run(MyTest)
(Where my junit test class is MyTest)
I've tried running:
MyTest
within the Groovy Console, but that just returns the class instance. Is there an easier way?

Related

Eclipse Photon: Test Suite results in "Np test found with test runner 'JUnit 5"." error message

In Eclipse Photon I have a project based on Java 1.8 with multiple Junit 5 Unit Tests. The individual unit tests work well and now I'm looking to generate a Junit Test Suite.
Based on the JUnit5 User Guide I have created a AllTest.java file that has the following code:
#RunWith(JUnitPlatform.class)
#SuiteDisplayName("Test Suite")
#SelectPackages("com")
public class AllTests
{
}
When I execute these Java file I get the following error message:
Screen Shot of Error Message
I've also tried the #SelectClasses and have run into the same issue.
Has anyone else seen this issue or know how to resolve it?
#RunWith(JUnitPlatform.class) is a JUnit4 annotation, so while executing the test we have to select RunConfiguration and select Junit 4 is the drop down instead of Junit 5. This will run the tests.

Selenium: method main requires 1 parameters but 0 were supplied in the #Test annotation

While executing the script using TestNG, it is skipping the main. Below is a sample code, which is similar to mine. If i remove String[] args, then the code is working with TestNG, but running as java application option is removed. How can i overcome this?
Package tests;
import org.testng.annotations.Test;
#Test
public class SearchText {
public void createzoo(String[] args) {
String[] elems = {"lion", "tiger", "duck"};
System.out.println(elems[0]);
System.out.println(elems[1]);
System.out.println(elems[2]);
}
}
Here is the Answer to your Question:
When you want to execute a Java Project you have 2 ways to do so:
Execute the Java file which contains the main method as:
public static void main(String[] args)
Your Java program will start executing from Line 1 within main method.
If you have integrated TestNG libraries to your Java Project you have got a new approach to execute your Java Project. Once you integrate TestNG there is absolutely no need of writing main method as previous case. In this case you need to execute the Class file which contains the #Test Annotation of TestNG as a TestNG Test. TestNG will execute your methods marked with #Test Annotations one by one as per the logic you specify. Once you execute your Java Project as a TestNG Test a xml file is generated by the name testng-customsuite.xml.
Next you can use your IDE feature Convert to TestNG to convert your Java Project to a TestNG specific Project. On doing that you will observe an xml file by the name testng.xml being created at your Project Level. Now using testng.xml you can also be able to execute the Project as a TestNG Suite
Let me know if this Answers your Question.
If you set #Test on a class, then it means that all methods of the class are test methods, included main.
If a test method has params, then TestNG must know how to inject them.
Currently, there is no way to exclude some method via annotations and an issue related issue already exists: https://github.com/cbeust/testng/issues/1405
As an alternative, you can only annotate methods instead of class and it should work.
This error "XYZ method requires 1 parameters but 0 were supplied in the #Test annotation" is encountered when there is misuse of #Test annotation in the code.
In your case, you can either run the application as java application or as TestNG application. When using TestNG , main is not required and you can use run as TestNG. When running as java application, main method would be required.
If i remove String[] args, then the code is working with TestNG, but running as java application option is removed.
When you remove String[] args, it will not be considered main method required for running java application. It is just any regular method. Hence it is working with TestNG but run as java application option is not applicable.

JVM Cucumber Execution

I am building a framework that is able to execute Selenium instructions and Cucumber instructions. I have created the runner for Cucumber in a seperate empty class with the following code
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
public class RunCukesTest {
}
I also will have a processor for Selenium instructions in a separate class.
prop.load(ClassLoader.getSystemResourceAsStream("src/main/resources/runConfig.properties"));
if(prop.getProperty("instructionsheet").contains("InstructionSheets")){
KeywordProcessor kp = new KeywordProcessor();
}else if(prop.getProperty("instructionsheet").contains("FeatureFiles")){
RunCukesTest runCukes = new RunCukesTest();
}
How can I execute the Cucumber runner class if it's an empty class?, do I just create an instance of the runner class for cucumber? or do I have to spacify something else
#RunWith(Cucumber.class) is used to integrate cucumber with the existing infrastructure for running JUnit tests. If you start to roll out your own framework, it is worth to look other places. cucumber.api.cli.Main could be a good place to start your journey.
The class RunCukesTest is a class that will be executed by the JUnit runner Cucumber.class since it is annotated with #RunWith(Cucumber.class).
In other words, it will be executed when you run the tests the same way as any other JUnit class.
The class is empty, and has to be empty, to force a separation of the execution of Cucumber and the steps supporting the execution.
If you want to execute Cucumber from another context, say your own framework, look at the command line interface. You are able to run its main() from any Java class, just call the static method and wait for the result.

Was woundering if there was a way to have a JUnit class that runs other JUnit testclasses

Was woundering if there was a way to have a JUnit class that runs other JUnit testclasses.
I have got a few testclasses, and i wan't to make a JUnit class that runs all the other test classes so i can just run the one JUnit class to run all the tests.
You probably need a TestSuite.
http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/TestSuite.html

Run single test from a JUnit class using command-line

I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java.
I can run the whole set of tests from the class using the following:
java -cp .... org.junit.runner.JUnitCore org.package.classname
What I really want to do is something like this:
java -cp .... org.junit.runner.JUnitCore org.package.classname.method
or:
java -cp .... org.junit.runner.JUnitCore org.package.classname#method
I noticed that there might be ways to do this using JUnit annotations, but I would prefer to not modify the source of my test classes by hand (attempting to automate this). I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.
So I am wondering if there is any way to do this?
Key points I'm looking for:
Ability to run a single test from a JUnit test class
Command Line (using JUnit)
Avoid modifying the test source
Avoid using additional tools
You can make a custom, barebones JUnit runner fairly easily. Here's one that will run a single test method in the form com.package.TestClass#methodName:
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
public class SingleJUnitTestRunner {
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
You can invoke it like this:
> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner
com.mycompany.product.MyTest#testB
After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn't produce any result output other than a non-zero exit code on test failure(s).
NOTE:
for JUnit version >= 4.9 you need hamcrest library in classpath
I use Maven to build my project, and use SureFire maven plugin to run junit tests.
Provided you have this setup, then you could do:
mvn -Dtest=GreatTestClass#testMethod test
In this example, we just run a test method named "testMethod" within Class "GreatTestClass".
For more details, check out http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
The following command works fine.
mvn -Dtest=SqsConsumerTest -DfailIfNoTests=false test
We used IntelliJ, and spent quite a bit of time trying to figure it out too.
Basically, it involves 2 steps:
Step 1: Compile the Test Class
% javac -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" SetTest.java
Step 2: Run the Test
% java -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" org.junit.runner.JUnitCore SetTest

Categories