JUnit - Run specific tests using JUnitCore - java

I have a couple of classes which contain Tests.
I have a main method that uses JUnitCore in order to run all tests.
What can I do in order to run specific tests in each class?
Currently I use something like this to run all my tests :
Result result = JUnitCore.runClasses(TestJunit.class, TestJunit2.class);
Maybe there is a possibility to categorize the relevant tests and then run them using JUnitCore ?? Thanks !

You can build a org.junit.runner.Request by providing class and method name and pass it to run method of JUnitCoreclass. This will execute given test of the specified class.
Request request = Request.method(TestClass.class, "methodName");
Result result = new JUnitCore().run(request);
You can check the result of test by invoking wasSuccessful() method available in Result class .

Related

Java pass arguments dynamically to a junit test

I'm doing a special junit test, that the params are introduced in the front-end of the application by the user and received in back-end of the application. And i want to generate junit test that use that information as parameters.
I saw some guide (like mykong guide and tutorial points) but most of them use static parametrized and i want some dynamic thing. I already tried to use junit annotations, do a set or pass the params to the junit class, use mockito methods but nothings work as dynamic process
Can someone point me to the right direction?
Right now i have something like that
public void run (Object foo) //Class that contains the information introduced by the user
JUnitCore junit1 = new JUnitCore();
Result result4 = JUnitCore.runClasses(GeneratedTest.getClass()); //Junit class
//I tried: do a setFoo on the GeneratedTest ; pass the foo on the constructor;
for (Failure failure : result4.getFailures()) {
System.out.println(failure.toString());
}
Probably not the nicest solution, but maybe an acceptable workaround:
generate your unit test so that it fetches parameters from System properties
run the generated JUnit test in its own JVM, and pass the parameters/properties on the command line

Failures are not collected in dynamic created test suite

I have a jar file that I reflect from it test classes (junit test).
I created TestSuite instance and added the tests to it.
In order to check my code, I've tried to add only one example test class to the test suite.
TestSuite suite = new TestSuite();
suite.addTest(new JUnit4TestAdapter(ExampleTest.class));
Then I called suite.run() in order to run the test:
TestResult result = new TestResult();
suite.run(result);
The problem is that when the test is done, the failures list that should be in result is empty.
Enumeration<TestFailure> failures = result.failures(); // = empty list
How can I get the failures using testSuite? If I use JUnitCore.runclasses , I do get the failures list but it can't be used with an instance of TestSuite, which I have to use since I get the test classes as input.
Edit-1:
If it is possible to create a Class that extends TestSuite and add classes to the suite dynamically, it will be good for my needs to.
Any suggestion will be great.
Edit-2:
From more searching in the web, I saw there us a difference between a failure and a testFailure. How can I fail a test with testFailure and not a failure?
After some more debugging, I found out that the TestResult object did catch the failures but reported them as Errors (Assertion Errors).
I also found out that TestResult.errors() returned an enumerator of TestFailure. Each TestFailure contain information about the thrown exception, so I can distinguish now between errors and failures by checking which of the errors are Assertion Errors and which are not.

How to run also ignored tests in JUnit 4 from command line?

I would like to run some ignored tests from a java program's main method in JUnit 4. They are ignored because they only insert some data for demonstration purpose.
in my main method:
JUnitCore junit = new JUnitCore();
junit.run(MyClass.class);
However this will not run test classes annotated with #Ignored
#Ignore("Only for filling system with demo data")
public class MyClass { ... }
In IntelliJ I can run those by right clicking on the class. How can I run them from the command line / in a main method?
I saw the Runner IgnoredClassRunner but not sure how to use that and if that is the correct Runner/Class.
It turned out this is quite easy achievable with:
JUnitCore junit = new JUnitCore();
junit.run(new BlockJUnit4ClassRunner(MyClass.class));
I will leave this answer open if anybody comes up with something better, which just prevents IgnoredClassRunner to be invoked for classes annotated with #Ignored

Pass a parameter with JUnitCore.run() method

I have one Test launcher class to run different type of tests by loading and executing classes generated to run with selenium. In this I am launching test by giving a string name and then running it with JUnitCore.run() method
Here is sample code for that:
Class clsTestStep = Class.forName("CheckLogin",true,new ClassLoader() {});
JUnitCore junit = new JUnitCore();
junit.addListener(new ExecutionListener());
org.junit.runner.Result runner= junit.run(clsTestStep);
Can Anyone tell me if I want to pass some object or property value with this class then How can I achieve it?
Note that I want that object to be available in 'CheckLogin' class at test running time, i.e username/password passed from launcher class to 'CheckLogin' class.

Pass command line arguments to JUnit test case being run programmatically

I am attempting to run a JUnit Test from a Java Class with:
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.run(classToRun);
Problem is my JUnit test requires a database connection that is currently hardcoded in the JUnit test itself.
What I am looking for is a way to run the JUnit test programmatically(above) but pass a database connection to it that I create in my Java Class that runs the test, and not hardcoded within the JUnit class.
Basically something like
JUnitCore core = new JUnitCore();
core.addListener(new RunListener());
core.addParameters(java.sql.Connection);
core.run(classToRun);
Then within the classToRun:
#Test
Public void Test1(Connection dbConnection){
Statement st = dbConnection.createStatement();
ResultSet rs = st.executeQuery("select total from dual");
rs.next();
String myTotal = rs.getString("TOTAL");
//btw my tests are selenium testcases:)
selenium.isTextPresent(myTotal);
}
I know about The #Parameters, but it doesn't seem applicable here as it is more for running the same test case multiple times with differing values. I want all of my test cases to share a database connection that I pass in through a configuration file to my java client that then runs those test cases (also passed in through the configuration file).
Is this possible?
P.S. I understand this seems like an odd way of doing things.
You can use java system properties to achieve this.
Simply pass what you need with -Dconnectionstring=foobar in the junit command line, or use the java api for system properties to set this programmatically, with System.setProperty(String name, String value), and System.getProperty(String name).
In your tests, you can use the #Before or #BeforeClass to set up common objects based on this property, pending on whether you want to run the setup once for each test (in which case you can use class members) or once for each suite (and then use static members).
You can even factorize this behavior by using an abstract class which all your test cases extends.

Categories