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.
Related
I created 2 Test classes (1 with 30 testcases, the other one with 10 testcases) in Apache Camel with Spring and Maven.
One of the Test class works fine, and runs if I build with Maven.
mvn test <- Works with one Test class
mvn package <- Works also with one Test class
But the other test class don't run. Both are in src/test/java
Also both test classes are in target/test-classes/...
One different is in target/test-classes/... the not working class have an second file with ...Test$1.class in the same folder. But I cannot open it.
And one second different have the classes: The not working class extends from CamelTestSupport and is a junit4. The working test-class is from junit5.
I think the maven-surefire-plugin don't find the junit4 test class. But how can I make maven-surefire-plugin finding junit4 test classes?
The JUnit 4 tests have to be executed with the Vintage engine of JUnit and the JUnit 5 tests with the Jupiter engine.
See this answer how to configure the POM to use both engines depending on the version of the surefire plugin.
I have a bunch of JUnit tests that extend my base test class called BaseTest which in turn extends Assert. Some of my tests have a #Category(SlowTests.class) annotation.
My BaseTest class is annotated with the following annotation #RunWith(MyJUnitRunner.class).
I've set up a Gradle task that is expected to run only SlowTests. Here's my Gradle task:
task integrationTests(type: Test) {
minHeapSize = "768m"
maxHeapSize = "1024m"
testLogging {
events "passed", "skipped", "failed"
outputs.upToDateWhen {false}
}
reports.junitXml.destination = "$buildDir/test-result"
useJUnit {
includeCategories 'testutils.SlowTests'
}
}
When I run the task, my tests aren't run. I've pinpointed this issue to be related to the custom runner MyJUnitRunner on the BaseTest. How can I set up my Gradle or test structure so that I can use a custom runner while using the Suite.
The solution to this turned out to smaller and trickier than I thought. Gradle was using my custom test runner and correctly invoking the filter method. However, my runner reloads all test classes through its own classloader for Javaassist enhancements.
This lead to the issue that SlowTest annotation was loaded through the Gradle classloader but when passed to my custom runner, the runner checked if the class was annotated with that annotation. This check never resolved correctly as the equality of the SlowTest annotation loaded through two different classloaders was different.
--
Since I've already done the research, I'll just leave this here. After days of digging through the Gradle and the (cryptic) JUnit sources, here's what I got.
Gradle simply doesn't handle any advanced JUnit functionality except the test categorization. When you create a Gradle task with the include-categories or the exclude-categories conditions, it builds a CategoryFilter. If you don't know, a Filter is what JUnit gives to the test-runner to decide whether a test or a test method should be filtered out. The test runner must implement the Filterable interface.
JUnit comes with multiple runners, the Categories is just another one of them. It extends a family of test runners called Suite. These suite based runners are designed to run a "suite" of tests. A suite of tests could be built by annotation introspection, by explicitly defining tests in a suite or any other method that builds a suite of tests.
In the case of the Categories runner, JUnit has it's own CategoryFilter but Gradle doesn't use that, it uses it's own CategoryFilter. Both provide more or less the same functionality and are JUnit filters so that can be used by any suite that implements Filterable.
The actual class in the Gradle responsible for running the JUnit tests is called JUnitTestClassExecuter. Once it has parsed the command line options it requests JUnit to check the runner should be used for a test. This method is invoked for every test as seen here.
The rest is simply up to JUnit. Gradle just created a custom RunNotifier to generate the standard XML files representing test results.
I hope someone finds this useful and saved themselves countless hours of debugging.
TLDR: You can use any runner in Gradle. Gradle has no specifics pertaining to runners. It is JUnit that decided the runners. If you'd like to know what runner will be used for your test, you can debug this by calling
Request.aClass(testClass).getRunner(). Hack this somewhere into your codebase and print it to the console. (I wasn't very successful in attaching a debugger to Gradle.)
A similar question has already been asked here.
One (unaccepted) answer states:
the test class will always be started directly and then through the
"link" in the suite. This is as expected.
Can someone explain what this actually means and whether or not it is possible to prevent the tests running twice.
When I run the tests from the command line using mvn test they only run once.
UPDATE
I have a test suite defined as follows:
#RunWith(Suite.class)
#SuiteClasses({ TestCase1.class, TestCase2.class })
public class MyTestSuite
{
}
When you run tests in Eclipse on project level (or package level), Eclipse searches all project's source folders for JUnit classes (or selected package). These are all classes with #Test annotations and all classes with #RunWith (probably some more too). Then for all these classes it runs them as tests.
As a result of this behavior, if you have a suite class that references tests classes in the same project, these tests will run twice. If you had another suite that did the same, they would run three times and so on. To understand this behavior try running a suite that contains one test case twice, for instance:
#RunWith(Suite.class)
#SuiteClasses({ TestCase1.class, TestCase1.class })
public class TestSuite {}
Accepted strategy here is to define a suite or suites for a project an run them exclusively. Do not start tests on a project level but run selected suites only.
As far as Maven is concerned, I suspect that its default configuration only picks out suite class and omits test cases. Had it been configured differently, it would behave the same as Eclipse.
Elipse tests 2 classes and give you 2 results.
Maven tests 2 classes and give you one result with 2 sub results.
I think is somethink like this, but still most important thing is that result are
positive! :)
Regards!
Same as this question https://github.com/spring-projects/spring-boot/issues/13750
Just exclude individual test cases and include the suite test cases.
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.
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 ...