I have an eclipse workspace (version 2019-12, non-maven) with multiple web projects, all containing java code, and some referencing each other. At the end of the reference 'graph' is a project dedicated to contain a test suite, which in turn is supposed to reference all other projects' tests.
All projects export their test-source folders. (All test classes are visible to the downstream projects).
I started to define the following suite:
package platformTest;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
import de.***.PlatformEnvironmentTestcases;
import de.***.ClientTestcases;
#RunWith(JUnitPlatform.class)
#SelectClasses({
PlatformEnvironmentTestcases.class,
ClientTestcases.class
})
public class JUnit5TestSuite {
// test suite
}
However the referenced test classes (though successfully running on their own, and being directly referenced) are not being executed at all.
I tried:
setting the debug config to junit4, with the result that no tests are executed. It seems the compatibility layer between junit 4 and 5 is not working here.
setting the debug config to junit5, with the result that a message window pops up: "No tests found with test runner 'JUnit 5'.
setting the debug config to junit5 and selecting 'Run all tests in the selected project, package or folder:' which - surprisingly - is working, but I cannot select the workspace root, which means I can never select all projects, and this setup will only find the tests in the suit's project and the selected project, but not the rest.
I found multiple bugs referenced for older versions of eclipse displaying similar symptoms, but since I am running a very recent version, I suppose those should not apply anymore. Also I tried most of the workarounds suggested without success.
Related
I am testing a Spring Boot application using code from a tutorial. The tutorial describes the setup and configuration of a Spring Boot application, and also describes a test that uses JAssert calls in the following manner:
package hello;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class SmokeTest {
#Autowired
private HomeController controller;
#Test
public void contexLoads() throws Exception {
assertThat(controller).isNotNull();
}
}
The test, unfortunately, will not compile in my IDE. The compilation is failing on the assertThat() method.
I am using Eclipse with Maven for my IDE. I have checked the Maven dependencies and see that the JAssert core library is included. Unfortunately, despite this the compiler can not seem to "find" the assertThat() call.
It fails to compile the test for that reason.
How do I get the test to utilize JAssert and find the calls to JAssert functions?
I'm not an eclipse expert, but it seems like you have some configuration issue here.
If you're sure that the dependency appears in pom.xml in scope test then try to eliminate eclipse related issues by running the tests directly via maven:
mvn test
will do the job
If it runs sucessfully, then re-create eclipse configurations from pom.xml / re-import the project.
If not, its a pom.xml related issue and has nothing to do with eclipse, you'll have to fix the pom or maven ecosystem. I suggest the following:
go to the local repository and remove the dependency from the file system (manually, the whole directory, with jar, pom.xml and everything) and then rerun mvn test
Sometimes dependencies are downloaded corrupted and despite being defined correctly in pom.xml they do not really contain classes in a form that java/maven can read
Actually:
It turns out that neither the Boot starter nor the IDE seems to find the necessary import declaration for the JAssert functions. Could it be because they are static?
In looking at some example code, I found in the code the import declaration for the assertThat() method. Usually, Eclipse would have suggested this declaration but it does not.
What is weird is that I put in the declaration by hand, and not only does this allow me to compile, but Eclipse, once the import is included, the code assistant makes proper assertThat() suggestions without problems!
There could be a bug somewhere, but that is beyond the scope of my particular problem. My test now compiles without problems.
I hope someone looking at this can figure out why Elipse's code assist does not work for this particular library properly. I wouldn't be surprised to discover that there are other objects/libraries in Spring Boot that don't get properly handled by the code assistant.
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.
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 ...
Hi – I wanted to automatically sweep JUnit tests into suites as part of my continuous builds, so I derived a runner from JUnit's Suite which finds all test classes in a package. The runner works just fine, but the results display is less than expected.
I have one class in my testing support package with a #RunWith annotation for my runner. The runner works by reading a property to get the package under test. Set the property and tell JUnit to run the annotated class, and all tests in that package are executed. The name of the suite is reported as the name of the class which has the #RunWith annotation, in both Ant and IntelliJ. My runner has an override for ParentRunner.getName() which returns the name of the package under test. I verified that the string gets into the runner's Description object. What am I missing?
Environment:
JUnit: 4.5
Ant: 1.7.0
IntelliJ IDEA: 8.1
Thanks for whatever direction you can provide.
This is because ANT and IntelliJ use their own runners, so they are building the name based on the test, and not getting the name from your runner. In other words, the runner is delegated to for the purpose of running the test, but not for the purpose of describing it.
I had a similar problem just a few weeks ago and created an open source project because of it.
You may include it via maven
<dependency>
<groupId>com.github.cschoell</groupId>
<artifactId>junit-dynamicsuite</artifactId>
<version>0.2.0</version>
<scope>test</scope>
</dependency>
or download it from the github page, where you will find its documentation as well.
https://github.com/cschoell/Junit-DynamicSuite
There is a Junit runner included which allows to scan either a directory or the classpath for unit tests and filter them by implementing a simple interface.