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

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.

Related

intellij is there a way to run multiple Junit tests at once right after a maven project is built?

For example, I have a class of Junit tests which inlcudes 100 tests. is there a way to automatically run all these tests right after I build the project? Is there any setting that I need to toggle on and off or any annotations I need to add to my test class to trigger that?
My current setting is below: it doesn't automatically run the test upon build
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class someTest {
#Test
#Order(1)
public void Test44(){
}
etc...
}
In IntelliJ you usually do it the other way round: You run all tests, which will trigger a re-build if required, ie if there are code or config changes that should trigger a rebuild.
IntelliJ chooses the build actions depending on you project’s build tool, e.g. Maven or Gradle.

How to set up environment for an Eclipse JUnit plugin test

There are a few questions such as this one which touch on the subject of running an Eclipse JUnit Plugin Test, but I have not been able to find an example of how to trigger some aspect of the plugin I'm testing.
I've followed this documentation which mentions a test harness, but I haven't found any more documentation on the harness. I can run JUnit tests as Eclipse Plugin Tests, but I'm not sure how to make them do useful things.
With that context, here are my specific questions:
How do I set up the context within which the plugin under test is invoked (e.g., "IResource xyz is selected")?
How do I get a handle to the plugin under test in order to invoke it? I'm assuming there's a way to invoke one of its extensions?
Can I programmatically inject resources into the Eclipse JUnit Test workspace?
I've managed to stumble my way through most of this, here are my steps and some sample code for anyone interested:
A plugin test needs to be defined in an Eclipse Plugin project. I prefer to do this in a separate project from the plugin I'm testing. If you follow this approach, you'll need to add the plugin you're testing as a dependency.
You create the test class like a regular JUnit test class.
You can run your test class by using the "Run as JUnit Plugin Test" run configuration. (Right click --> Run as --> JUnit Plugin Test). This will create a new instance of Eclipse and run your test inside it.
Below is some sample code for a test that creates some resources in the junit workspace using content from the test plugin's src directory, then makes some assertions.
Setting up resources:
package org.sampletest;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.junit.Test;
import com.example.myplugin.MyPlugin
public class MyPluginTest{
#Test
public void testMyPlugin() throws Exception{
// assume an empty workspace - "Run as JUnit Plugin Test can be configured to clear the workspace before each run.
String name = "myPluginTestProject";
IProjectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(name);
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
project.create(desc, new NullProgressMonitor());
project.open(new NullProgressMonitor());
IFile file = project.getFile("myPluginTestFile");
InputStream source = getClass().getClassLoader().getResourceAsStream("sampleFile");
file.create(source, IFile.FORCE, null);
// Assuming MyPlugin has some operation on an IStructuredSelection
MyPlugin plugin = new MyPlugin();
IStructuredSelection selection = new StructuredSelection(file);
plugin.invoke(selection);
// Make some assertions...
}
}

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.

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.

JUnit 4 Test Suites

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 ...

Categories

Resources