JUnit 4 Test Suites - java

How do I create test suites with JUnit 4?
All the documentation I've seen doesn't seem to be working for me. And if I use the Eclipse wizard it doesn't give me an option to select any of the test classes I have created.

import org.junit.runners.Suite;
import org.junit.runner.RunWith;
#RunWith(Suite.class)
#Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class TestSuite {
//nothing
}

You can create a suite like so. For example an AllTest suite would look something like this.
package my.package.tests;
#RunWith(Suite.class)
#SuiteClasses({
testMyService.class,
testMyBackend.class,
...
})
public class AllTests {}
Now you can run this in a couple different ways:
right-click and run in Eclipse as Junit test
create a runable Java Application; Main class='org.junit.runner.JUnitCore' and Args='my.package.tests.AllTests'
run from the command line:
$ java -cp build/classes/:/usr/share/java/junit4.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore my.package.tests.AllTests

I think TestSuite has fallen out of favor. That might have been the style before 4.x, but it's not now as far as I know.
I just annotate the tests I want and then run the class. All the annotated tests are run. I might use Ant, but most of the time I have IntelliJ run them for me.

Here are the steps to create a JUnit suite in eclipse:
In the 'Package Explorer' view of the eclipse 'Java' perspective,
select your unit test(s) in their package, inside the eclipse java
project.
Right-click on any one of the selected tests.
In the pop-up menu, select New, Other…
Open the ‘Java’ folder, then open the ‘JUnit’ folder
Select ‘JUnit Test Suite’ and then select the ‘Next’ button
Select button ‘Finish’
Result: ‘AllTests.java’ suite file is created, with tests automatically
included
Select the Run button in eclipse
Result: all tests in suite run
You can now point to this suite file with ANT, Jenkins or other build configuration continuous integration tool.
Version info: this is for eclipse Neon and JUnit 4. You can also select JUnit 3 before selecting 'Finish' in step 6.

Of the top of my head create a TestSuite and the invoke addTests. If you want somesource to look at try any opensource lib like hibernate or something from apache and take a look under the test directory of the source for a Tests suite ...

Related

Exporting multiple JUnit classes into single executable jar

I am trying to export JUnit classes into 1 executable JAR. I can't do this because it doesn't have a main.
What I have tried:
I tried making a testingSuite but that did not work as well. I can run the JUnit class from Eclipse, I can also run the testingSuite - and it calls all JUnit classes I tell it too - they work fine in eclipse. Note, I had to go down to JUnit4 to use the testingSuite. Since I could not export the testingSuite either, I tried making a new class with one main method that calls the testingSuite, I cannot get this to run from Eclipse.
I have been going through Stack overflow and other sites for about 2 days, so now I will post =).
Anyone know how I can export multiple JUnit test classes into 1 executable Jar that can run all the classes when it is opened?
If you're goal is to run some selenium tests and if your tests arn't too big, why not use selenium ide (firefox plugin, and here for chrome)?
It depends on if you want these tests to be maintainable and evolutive but if they're just there to check things still work, give it a try. Plus it will allow your BA to write their own tests. No need to know about programming, just click. Sort of.
This whole end to end test thing is very expensive to maintain but if your app doesn't evolves too much on the surface (its UI) then it might be worthy.
For an in depth article about testing in general, including testing pyramid, read this by Martin Fowler, it's very good.
I was able to make it work by having a regular class call my Test Suite Class which calls my JUnit Test Classes. I don't know why it wasn't working before, but this time when I tried to export and there was a new option there.
Solution Below
JUnit test suite class (runs all the test classes I put into #SuiteClasses, called by 'TestRunner' class)
package myPackageName;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
#RunWith(Suite.class)
#SuiteClasses({ TestClass1.class, TestClass2.class })
public class AllTests {
}
TestRunner Class, the class that is exported into the executable jar. This was the missing piece, without it, export would not work.
package myPackageName;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(AllTests.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
Export Steps
Click File, Click Export
Open java Folder, Click 'Runnable JAR file', Click next
Launch Configuration drop down shows an option 'myPackageName - TestRunner'. This is where I was able to pick the class that contains the main method that will be run by the JAR. (the issue I was having before, it wasn't there and if I selected other classes that appeared it gave an export error).
I used the 'package required libraries into generated JAR' option for library handling, I think its correct because I have selenium libraries.
Click Finish
Run JAR by opening windows explorer and clicking it. Or, open CMD, cd to file directory, and run java -jar myJarName.jar.

Can you run all JUnit tests in a package from the command line without explicitly listing them?

Provided the test classes and JUnit are both on the classpath, one can run JUnit tests from the command line as follows:
java org.junit.runner.JUnitCore TestClass1 TestClass2
Now, is there a way to run all tests in a package (and sub-packages) as well?
I'm looking for something like
java org.junit.runner.JUnitCore com.example.tests.testsIWantToRun.*
Is there an easy way of doing that (that doesn't involve maven or ant)?
Junit lets you define suites of tests. Each suite defines a collection of tests, and running the suite causes all of the tests to be run. What I do is to define a suite for each package, listing the test classes for that package along with the suites for any sub-packages:
package com.foo.bar;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import com.foo.bar.baz.Suite_baz;
#RunWith(Suite.class)
#Suite.SuiteClasses({
ThisTest.class,
ThatTest.class,
TheOtherTest.class,
Suite_baz.class,
})
public class Suite_bar {
}
This isn't completely effortless. You have to construct the suites and manually update them with new test classes. I suppose it wouldn't be hard to write a little java program to generate these automatically, if someone wanted to.
I asked this question to be able to kick sets of a project's Cucumber tests on Jenkins off selectively, without really knowing what their RunTests classes would be called, what their CucumberOptions would contain, or where they would be located. I found a few helpful threads on StackOverflow in the meantime, which answer my question:
How do I Dynamically create a Test Suite in JUnit 4?
How to run multiple test classes with junit from command line?
Using those, I can kick my Cucumber tests off individually as follows:
First, I used the maven assembly plugin to get the tests packaged in a jar:
https://stackoverflow.com/a/574650/2018047
Then I copied the tests' dependencies to the target folder on Jenkins, as shown here:
https://stackoverflow.com/a/23986765/2018047
We already have a flag that skips the execution of our tests when it's set, so I package my tests without running them:
mvn clean install -DskipMyTestModule=true
And using the code from above and the invocation from below, I'll be able to make it all work...
java -Dcucumber.options="src/test/resources/features --tags #b --format pretty:STDOUT --format html:target/cucumber-b --format json:target/cucumber-b.json" -Dname=value -cp target/artifact-1.2.8-SNAPSHOT-tests.jar;target/test-classes/libs/junit-4.11.jar;target/test-classes/libs/* org.junit.runner.JUnitCore com.example.foo.bar.test.cucumber.RunTest
Hope this helps someone in the future. :)
With JUnit 4 this is supported using an extension called cpsuite. All you need to do is add it to your test classpath (maven io.takari.junit:takari-cpsuite), create a dynamic test suite class:
package com.mycompany;
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.runner.RunWith;
#RunWith(ClasspathSuite.class)
#ClasspathSuite.IncludeJars(true)
public class RunAllTests {}
and run it:
java -cp ${CP} org.junit.runner.JUnitCore com.mycompany.RunAllTests
Your classpath ${CP} should include your test jar, junit, hamcrest and cpsuite.

Why is my JUnit test not running in Eclipse?

It's been 3 hours now and I still didn't find a solution, even though I seem to have read all related questions already.
I am building an Android application and I just want to create a couple of simple Unit Tests that test my basic functions. I don't need to test any Android related logic or activity features.
So I have created a new directory in my solution in which I have created a new JUnit Test Case.
To keep things simple my test methods are not testing much yet, but even when doing a Right Click > Run As > JUnit Test, it's not doing anything.
As you can see in my screenshot the JUnit pane on the left shows my test is terminated but does not show any test that has been executed.
I have created a simple Unit Test in a new Java Project and then it's working. If I repeat the same steps in a new Android Application Project it's not working.
What do I need to do to run my simple Unit Tests?!
Thanks!
(My Compiler Compliance Level is 1.6)
Go to Window -> Show View -> Error Log to see what the actual error is.
For my case it was No test found with test runner 'Junit 5'.
Then one can google for respective solution.
You either don't have JUnit on the build path or you don't have the library (jar) at hand. Make sure both are in place.
I think you will need an unit test suite if all else fails:
#RunWith(Suite.class)
#SuiteClasses({ GpsLocationTest.class })
public class AllTests {
}
There is one more thing you can do: check whether you import the right #Test annotation. Restart Eclipse and clean your project if the problem persists.
You may want to refer to the vogella guide about unit testing.
You can use AndroidTestCase, which inherits from junit.framework.TestCase, not org.junit.Test.
This is related to Memory Issue.
Simply add these line in VM argument:
right click on Junit Test -> Run as -> Run Configuration -> Arguments -> add "-XX:MaxPermSize=512m" under VM argument
The java Build path of the project has Junit4.jar lets say and the Run configuration for the test you are running has Junit5 - then it causes to terminate and nothing happens.

How to create a Test Suite in Eclipse under org.junit

org.junit.Assert. has deprecated junit.framework.Assert
My question is how do I create a Test Suite in Eclipse if my JUnit classes do not extend a TestCase?
When I try to create new Test Suite in Eclipse my classes do not appear in the select box and I imagine this is because they don't extend a TestCase.
I thought that with the new org.junit I can just use annotation and not extend TestCase
The following code will create a test suite with Junit4. You can then just run this in Eclipse as a Junit test case. Obviously the TestClasses need to contain methods annotated with #Test otherwise no tests will actually run for the suite.
import org.junit.runners.Suite;
#RunWith(Suite.class)
#Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class TestSuite {
//nothing
}
JUnit 4, which is bundled with eclipse doesn't use the old way of testing, where your testcases and testsuites used to extend classes.
JUnit 4 relies on annotations.
Just right click your project, select "new->other" go to "Test case", select JUnit 4, select the class under test, the methods under test and you've got your test case.
For a test suite, right click the project, select "other", go to "test suite", and select the test case you created in the previous step... or more test cases.

How do I make Eclipse recognize JUnit tests when creating a suite?

When I use Eclipse to create a JUnit test suite, it does not detect any existing tests, and warns "No test classes selected."
I started from the test class package (test/com/.../package), and the package is selected. There are several JUnit tests there, also created through the same version of Eclipse, but there is no way to select them.
I am using JUnit 4.
Thanks!
The wizard for creating a suite currently only works with JUnit 3 tests, see corresponding bugzilla entry.
A Suite that works for me is :
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
...
#RunWith(Suite.class)
#SuiteClasses( { MyTest.class })
public class SeleniumSuite {
...
}
This helps if you want to run just a subset of tests defined in a package. What are you tests called? Try re-factoring them so they are called either Test*.java or *Test.java.
In Eclipse, you can simply right-click on the project / package you want to run tests in and select Run as > Junit Test - you can avoid needing to programmatically create a test suite class completely.

Categories

Resources