how to initialize JUnit test classes before running in parallel - java

I am trying to execute JUnit tests in parallel using the ParallelComputer experimental feature, however I do not know how to pre-initialize each of the test classes before kicking off the tests.
The classic example of how these are supposed to be used is as follows (as shown in the following GitHub link). Per the example, how do I initialize the ParallelTest1 and ParallelTest2 classes with shared data before starting the parallel thread testing. I was able to do this via constructors in each of the classes, however I need to make sure that both classes are fully initialized before the run starts. This problem is probably not specific to the Parallel nature of how I wish to perform the testing but more likely how to use some special keywords to order prevent initialized objects from starting until required. Ideally the example ParallelComputerTest could have this shared data initialized in its constructor, however in that case, how could the nested static parallel test classes get access to this instance data?
public class ParallelComputerTest {
#Test
public void test() {
Class[] cls={ParallelTest1.class,ParallelTest2.class };
//Parallel among classes
JUnitCore.runClasses(ParallelComputer.classes(), cls);
//Parallel among methods in a class
JUnitCore.runClasses(ParallelComputer.methods(), cls);
//Parallel all methods in all classes
JUnitCore.runClasses(new ParallelComputer(true, true), cls);
}
public static class ParallelTest1{
#Test public void a(){}
#Test public void b(){}
}
public static class ParallelTest2{
#Test public void a(){}
#Test public void b(){}
}
}

Use #Before for set ups and #After for clean ups.
For example to test console output I set up streams before and clean result after test like this:
#Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
#After
public void cleanUpStreams() {
System.setOut(null);
System.setErr(null);
}
NOTE: this can cause problems with TestSuite, dunno if also with ParallelTest. If you experience some troubles AND you use JUnit 4.7 or higher you might like to check this link to rules feature

Related

Java code is not printing from start to end [duplicate]

I want to execute test methods which are annotated by #Test in specific order.
For example:
public class MyTest {
#Test public void test1(){}
#Test public void test2(){}
}
I want to ensure to run test1() before test2() each time I run MyTest, but I couldn't find annotation like #Test(order=xx).
I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?
I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?
I'm not sure there is a clean way to do this with JUnit, to my knowledge JUnit assumes that all tests can be performed in an arbitrary order. From the FAQ:
How do I use a test fixture?
(...) The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). (...)
Why is it so? Well, I believe that making tests order dependent is a practice that the authors don't want to promote. Tests should be independent, they shouldn't be coupled and violating this will make things harder to maintain, will break the ability to run tests individually (obviously), etc.
That being said, if you really want to go in this direction, consider using TestNG since it supports running tests methods in any arbitrary order natively (and things like specifying that methods depends on groups of methods). Cedric Beust explains how to do this in order of execution of tests in testng.
If you get rid of your existing instance of Junit, and download JUnit 4.11 or greater in the build path, the following code will execute the test methods in the order of their names, sorted in ascending order:
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
#Test
public void testAcreate() {
System.out.println("first");
}
#Test
public void testBupdate() {
System.out.println("second");
}
#Test
public void testCdelete() {
System.out.println("third");
}
}
Migration to TestNG seems the best way, but I see no clear solution here for jUnit. Here is most readable solution / formatting I found for jUnit:
#FixMethodOrder( MethodSorters.NAME_ASCENDING ) // force name ordering
public class SampleTest {
#Test
void stage1_prepareAndTest(){};
#Test
void stage2_checkSomething(){};
#Test
void stage2_checkSomethingElse(){};
#Test
void stage3_thisDependsOnStage2(){};
#Test
void callTimeDoesntMatter(){}
}
This ensures stage2 methods are called after stage1 ones and before stage3 ones.
P.S. I feel this approach is better that jUnit 5.5 #Order annotation because it provides better notation for reader.
If the order is important, you should make the order yourself.
#Test public void test1() { ... }
#Test public void test2() { test1(); ... }
In particular, you should list some or all possible order permutations to test, if necessary.
For example,
void test1();
void test2();
void test3();
#Test
public void testOrder1() { test1(); test3(); }
#Test(expected = Exception.class)
public void testOrder2() { test2(); test3(); test1(); }
#Test(expected = NullPointerException.class)
public void testOrder3() { test3(); test1(); test2(); }
Or, a full test of all permutations:
#Test
public void testAllOrders() {
for (Object[] sample: permute(1, 2, 3)) {
for (Object index: sample) {
switch (((Integer) index).intValue()) {
case 1: test1(); break;
case 2: test2(); break;
case 3: test3(); break;
}
}
}
}
Here, permute() is a simple function which iterates all possible permuations into a Collection of array.
JUnit since 5.5 allows #TestMethodOrder(OrderAnnotation.class) on class and #Order(1) on test-methods.
JUnit old versions allow test methods run ordering using class annotations:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
#FixMethodOrder(MethodSorters.JVM)
#FixMethodOrder(MethodSorters.DEFAULT)
By default test methods are run in alphabetical order. So, to set specific methods order you can name them like:
a_TestWorkUnit_WithCertainState_ShouldDoSomething
b_TestWorkUnit_WithCertainState_ShouldDoSomething
c_TestWorkUnit_WithCertainState_ShouldDoSomething
Or
_1_TestWorkUnit_WithCertainState_ShouldDoSomething
_2_TestWorkUnit_WithCertainState_ShouldDoSomething
_3_TestWorkUnit_WithCertainState_ShouldDoSomething
You can find examples here.
Its one of the main issue which I faced when I worked on Junit and I came up with following solution which works fine for me:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
public class OrderedRunner extends BlockJUnit4ClassRunner {
public OrderedRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
#Override
protected List<FrameworkMethod> computeTestMethods() {
List<FrameworkMethod> list = super.computeTestMethods();
List<FrameworkMethod> copy = new ArrayList<FrameworkMethod>(list);
Collections.sort(copy, new Comparator<FrameworkMethod>() {
#Override
public int compare(FrameworkMethod f1, FrameworkMethod f2) {
Order o1 = f1.getAnnotation(Order.class);
Order o2 = f2.getAnnotation(Order.class);
if (o1 == null || o2 == null) {
return -1;
}
return o1.order() - o2.order();
}
});
return copy;
}
}
also create a interface like below:
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.METHOD})
public #interface Order {
public int order();
}
Now suppose you have class A where you have written several test cases like below:
(#runWith=OrderRunner.class)
Class A{
#Test
#Order(order = 1)
void method(){
//do something
}
}
So execution will start from method named "method()".
Thanks!
JUnit 5 update (and my opinion)
I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?
By default, unit testing libraries don't try to execute tests in the order that occurs in the source file.
JUnit 5 as JUnit 4 work in that way. Why? Because if the order matters it means that some tests are coupled between them and that is undesirable for unit tests.
So the #Nested feature introduced by JUnit 5 follows the same default approach.
But for integration tests, the order of the test method may matter since a test method may change the state of the application in a way expected by another test method.
For example when you write an integration test for a e-shop checkout processing, the first test method to be executed is registering a client, the second is adding items in the basket and the last one is doing the checkout. If the test runner doesn't respect that order, the test scenario is flawed and will fail.
So in JUnit 5 (from the 5.4 version) you have all the same the possibility to set the execution order by annotating the test class with #TestMethodOrder(OrderAnnotation.class) and by specifying the order with #Order(numericOrderValue) for the methods which the order matters.
For example :
#TestMethodOrder(OrderAnnotation.class)
class FooTest {
#Order(3)
#Test
void checkoutOrder() {
System.out.println("checkoutOrder");
}
#Order(2)
#Test
void addItemsInBasket() {
System.out.println("addItemsInBasket");
}
#Order(1)
#Test
void createUserAndLogin() {
System.out.println("createUserAndLogin");
}
}
Output :
createUserAndLogin
addItemsInBasket
checkoutOrder
By the way, specifying #TestMethodOrder(OrderAnnotation.class) looks like not needed (at least in the 5.4.0 version I tested).
Side note
About the question: is JUnit 5 the best choice to write integration tests? I don't think that it should be the first tool to consider (Cucumber and co may often bring more specific value and features for integration tests) but in some integration test cases, the JUnit framework is enough. So that is a good news that the feature exists.
The (as yet unreleased) change https://github.com/junit-team/junit/pull/386 introduces a #SortMethodsWith. https://github.com/junit-team/junit/pull/293 at least made the order predictable without that (in Java 7 it can be quite random).
Look at a JUnit report. JUnit is already organized by package. Each package has (or can have) TestSuite classes, each of which in turn run multiple TestCases. Each TestCase can have multiple test methods of the form public void test*(), each of which will actually become an instance of the TestCase class to which they belong. Each test method (TestCase instance) has a name and a pass/fail criteria.
What my management requires is the concept of individual TestStep items, each of which reports their own pass/fail criteria. Failure of any test step must not prevent the execution of subsequent test steps.
In the past, test developers in my position organized TestCase classes into packages that correspond to the part(s) of the product under test, created a TestCase class for each test, and made each test method a separate "step" in the test, complete with its own pass/fail criteria in the JUnit output. Each TestCase is a standalone "test", but the individual methods, or test "steps" within the TestCase, must occur in a specific order.
The TestCase methods were the steps of the TestCase, and test designers got a separate pass/fail criterion per test step. Now the test steps are jumbled, and the tests (of course) fail.
For example:
Class testStateChanges extends TestCase
public void testCreateObjectPlacesTheObjectInStateA()
public void testTransitionToStateBAndValidateStateB()
public void testTransitionToStateCAndValidateStateC()
public void testTryToDeleteObjectinStateCAndValidateObjectStillExists()
public void testTransitionToStateAAndValidateStateA()
public void testDeleteObjectInStateAAndObjectDoesNotExist()
public void cleanupIfAnythingWentWrong()
Each test method asserts and reports its own separate pass/fail criteria.
Collapsing this into "one big test method" for the sake of ordering loses the pass/fail criteria granularity of each "step" in the JUnit summary report. ...and that upsets my managers. They are currently demanding another alternative.
Can anyone explain how a JUnit with scrambled test method ordering would support separate pass/fail criteria of each sequential test step, as exemplified above and required by my management?
Regardless of the documentation, I see this as a serious regression in the JUnit framework that is making life difficult for lots of test developers.
Not sure I agree, If I want to test 'File Upload' and then test 'Data Inserted by File Upload' why would I not want these to be independent from each other? Perfectly reasonable I think to be able to run them separately rather than having both in a Goliath test case.
What you want is perfectly reasonable when test cases are being run as a suite.
Unfortunately no time to give a complete solution right now, but have a look at class:
org.junit.runners.Suite
Which allows you to call test cases (from any test class) in a specific order.
These might be used to create functional, integration or system tests.
This leaves your unit tests as they are without specific order (as recommended), whether you run them like that or not, and then re-use the tests as part of a bigger picture.
We re-use/inherit the same code for unit, integration and system tests, sometimes data driven, sometimes commit driven, and sometimes run as a suite.
JUnit 4 update
As of JUnit 4.13 #OrderWith, it is possible to reproduce the JUnit 5 #Order annotation. This solution better integrates with JUnit 4 than #RunWith a custom BlockJUnit4ClassRunner implementation.
Here's how I could replace method name ordering (#FixMethodOrder(MethodSorters.NAME_ASCENDING)) with an ordering by annotation.
#OrderWith(OrderAnnotation.class)
public class MyTest {
#Test
#Order(-1)
public void runBeforeNotAnnotatedTests() {}
#Test
public void notAnnotatedTestHasPriority0() {}
#Test
#Order(1)
public void thisTestHasPriority1() {}
#Test
#Order(2)
public void thisTestHasPriority2() {}
}
/**
* JUnit 4 equivalent of JUnit 5's {#code org.junit.jupiter.api.Order}
*/
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.METHOD })
public #interface Order {
/**
* Default order value for elements not explicitly annotated with {#code #Order}.
*
* #see Order#value
*/
int DEFAULT = 0;
/**
* The order value for the annotated element.
* <p>Elements are ordered based on priority where a lower value has greater
* priority than a higher value. For example, {#link Integer#MAX_VALUE} has
* the lowest priority.
*
* #see #DEFAULT
*/
int value();
}
import org.junit.runner.Description;
import org.junit.runner.manipulation.Ordering;
import org.junit.runner.manipulation.Sorter;
/**
* Order test methods by their {#link Order} annotation. The lower value has the highest priority.
* The tests that are not annotated get the default value {#link Order#DEFAULT}.
*/
public class OrderAnnotation extends Sorter implements Ordering.Factory {
public OrderAnnotation() {
super(COMPARATOR);
}
#Override
public Ordering create(Context context) {
return this;
}
private static final Comparator<Description> COMPARATOR = Comparator.comparingInt(
description -> Optional.ofNullable(description.getAnnotation(Order.class))
.map(Order::value)
.orElse(Order.DEFAULT));
}
The not annotated tests get a default priority of 0. The order of tests with the same priority is undetermined.
Gist: https://gist.github.com/jcarsique/df98e0bad9e88e8258c4ab34dad3c863
Inspired by:
Aman Goel's answer
Test execution order Wiki by JUnit team
JUnit 5 source code
See my solution here:
"Junit and java 7."
In this article I describe how to run junit tests in order - "just as in your source code".
Tests will be run, in order as your test methods appears in class file.
http://intellijava.blogspot.com/2012/05/junit-and-java-7.html
But as Pascal Thivent said, this is not a good practise.
As others have stated, tests should be ideally be independent of execution order. This makes tests less fragile, and allows them to be run independently (many IDEs allow you to select a test method and execute it independently of other tests).
That being said, for integration tests, some people prefer to rely on method ordering.
Starting with JUnit 4.13 you can define your own class to reorder tests by extending Ordering. See the JUnit wiki for more details. Here's an example using the built-in Alphanumeric class to order the tests alphanumerically using the test method name:
import org.junit.Test;
import org.junit.runner.OrderWith;
import org.junit.runner.manipulation.Alphanumeric;
#OrderWith(Alphanumeric.class)
public class TestMethodOrder {
#Test
public void testA() {
System.out.println("first");
}
#Test
public void testB() {
System.out.println("second");
}
#Test
public void testC() {
System.out.println("third");
}
}
For JUnit 4, putting this annotation on the test class solved the problem.
#FixMethodOrder(MethodSorters.JVM)
With JUnit 5.4, you can specify the order :
#Test
#Order(2)
public void sendEmailTestGmail() throws MessagingException {
you just need to annotate your class
#TestMethodOrder(OrderAnnotation.class)
https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order
i'm using this in my project and it works very well !
You can use one of these piece of codes:
#FixMethodOrder(MethodSorters.JVM) OR #FixMethodOrder(MethodSorters.DEFAULT) OR #FixMethodOrder(MethodSorters.NAME_ASCENDING)
Before your test class like this:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BookTest {...}
It's time to move to Junit5.
Here is a sample of what we could get:
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class OrderedTests {
#Test
#Order(1)
void nullValues() {}
#Test
#Order(2)
void emptyValues() {}
#Test
#Order(3)
void validValues() {}
}
For Junit4, copy the logic that you have in several tests in one test method.
I've read a few answers and agree its not best practice, but the easiest way to order your tests - and the way that JUnit runs tests by default is by alphabetic name ascending.
So just name your tests in the alphabetic order that you want. Also note the test name must begin
with the word test. Just watch out for numbers
test12 will run before test2
so:
testA_MyFirstTest
testC_ThirdTest
testB_ATestThatRunsSecond
Please check out this one: https://github.com/TransparentMarket/junit. It runs the test in the order they are specified (defined within the compiled class file). Also it features a AllTests suite to run tests defined by sub package first. Using the AllTests implementation one can extend the solution in also filtering for properties (we used to use #Fast annotations but those were not published yet).
Here is an extension to JUnit that can produce the desired behavior: https://github.com/aafuks/aaf-junit
I know that this is against the authors of JUnit philosophy, but when using JUnit in environments that are not strict unit testing (as practiced in Java) this can be very helpful.
I ended up here thinking that my tests weren't run in order, but the truth is that the mess was in my async jobs. When working with concurrency you need to perform concurrency checks between your tests as well.
In my case, jobs and tests share a semaphore, so next tests hang until the running job releases the lock.
I know this is not fully related to this question, but maybe could help targeting the correct issue
If you want to run test methods in a specific order in JUnit 5, you can use the below code.
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MyClassTest {
#Test
#Order(1)
public void test1() {}
#Test
#Order(2)
public void test2() {}
}

How to reuse method and test in JUnit?

I've tried to avoid duplicate code in JUnit test, but I'm kind of stuck.
This is my first test, for the second one it has exactly the same methods but different service (different input). instead of the TestCaseResourceTest1 I have TestCaseResourceTest2. Now what could be the proper way to test both? I want to have a separate file for test number 2, how should I avoid the duplicate code? (ex. use the beforeFileTest() method)
public class TestCaseResourceTest1 {
#Mock
private TestService testService;
#Mock
private AreaService areaService;
private TestCaseService1 testCaseService1; // is changed in test2
#Before
public void before() throws Exception{
testCaseService1 = mock(TestCaseService1.class); // is changed in test2
MockitoAnnotations.initMocks(this);
beforeFileTest();
}
private void beforeFileTest() throws Exception{
doReturn(true).when(areaService).chechExists(any(String.class), eq(false));
}
#Test
public void verifyFileExists() throws Exception{
verifyOtherArea(testCaseService1); // is changed in test2
doReturn(false).when(areaService).chechExists(any(String.class), eq(false));
}
}
just lines with comment is changed in test2 are differences.
Tnx
Given this excerpt from your question:
… instead of the TestCaseResourceTest1 I have TestCaseResourceTest2 … I want to have a separate file for test number 2
… the standard ways of sharing code between test cases are:
Create a Test Suite and include the shared code in the test suite (typically in #BeforeClass and #AfterClass methods). This allows you to (1) run setup code once (per suite invocation); (2) encapsulate shared setup/teardown code and (3) easily add more tests cases later. For example:
#RunWith(Suite.class)
#Suite.SuiteClasses({
TestCaseResourceTest1.class,
TestCaseResourceTest2.class
)}
public class TestSuiteClass {
#BeforeClass
public void setup() {
beforeFileTest();
}
private void beforeFileTest() throws Exception {
// ...
}
}
Create an abstract class which parents TestCaseResourceTest1 and TestCaseResourceTest2 and let those test cases call the shared code in the parent (typically via super() calls). With this approach you can declare default shared code in the parent while still allowing sub classes to (1) have their own behaviour and (2) selectively override the parent/default behaviour
Create a custom JUnit runner, define the shared behaviour in this runner and then annotate the relevant test cases with #RunWith(YourCustomRunner.class). More details on this approach here
Just to reiterate what some of the other posters have said; this is not a common first step so you may prefer to start simple and only move to suites or abstract classes or custom runners if your usage provides a compelling reason to do so.
I had the such situation and it was a sign about wrong implementation design. We are talking about pure unit tests where we test exactly what is implemented in the production classes. If we need duplicated tests it means we probably have duplication in implementation.
How did I resolve it in my project?
Extracted common logic into parent service class and implemented unit tests for it.
For child services I implemented tests only for particular implemented code there. No more.
Implemented an integration tests on real environment were both services were involved and tested completely.
Assuming you want to have the exact same test run for 2 different classes (and not mocking it as in your example code), you can create an abstract test class, that has abstract method that returns an instance of the class to be tested.
Something in the vein of:
public abstract class TestCaseResourceTest {
protected abstract TestCaseService1 getServiceToTest();
#Before
public void before() throws Exception {
testCaseService1 = getServiceToTest();
MockitoAnnotations.initMocks(this);
beforeFileTest();
}
#Test
public void test() {
// do your test here
}
}
public class ConcreteTest extends TestCaseResourceTest {
protected TestCaseService1 getServiceToTest() {
return new TestCaseService();
}
}
public class ConcreteTest2 extends TestCaseResourceTest {
protected TestCaseService1 getServiceToTest() {
return new DifferentService();
}
}
Have you considered using JUnit 5 with its http://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests ?
It allows you to re-use your tests with different input. This is an example from the documentation which illustrates what you can do now with JUnit 5:
#ParameterizedTest
#ValueSource(strings = { "Hello", "World" })
void testWithStringParameter(String argument) {
assertNotNull(argument);
}
But you can also create your methods which return the input data:
#ParameterizedTest
#MethodSource("stringProvider")
void testWithSimpleMethodSource(String argument) {
assertNotNull(argument);
}
static Stream<String> stringProvider() {
return Stream.of("foo", "bar");
}
Here I am using just strings, but you can really use any objects.
If you are using Maven, you can add these dependencies to start using JUnit 5:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.0.0-RC2</version>
<scope>test</scope>
</dependency>
The only annoying thing about JUnit 5 is that it is not released yet.
When going from one test to two tests, you don't know what will be duplicate code, so I find it useful to put everything into one test method. In this case, start by putting the contents of the #Before and beforeFileTest methods inline in the test.
Then you can see that it is just te service that needs changing, so you can extract everything except that into a helper method that is called from two tests.
Also, after you have two tests that are calling the same helper method and are happy with that test coverage, you could look into writing parameterized tests. For example with JunitParams: https://github.com/Pragmatists/junitparams/wiki/Quickstart

Junit 4 how to initialize in test suite and pass it to test classes

Is there a way in Junit4 to pass some pre-initialized data like http connections that you initialize using say '#ClassRule' in the class annotated with '#RunWith(Suite.class)' to all child (i.e. suite) test classes? Also how to ensure that the classes in the suite cannot be run individually?
e.g.
class A {
private HttpClient client;
#Test
public void someTest() {
client.execute(...);
...
}
}
#RunWith(Suite.class)
#Suite.SuiteClasses({A.class})
public class Suite {
private static HttpClient client;
#ClassRule
public static ExternalResource resource= new ExternalResource() {
#Override
protected void before() throws Throwable {
client = new DefaultHttpClient();
}
}
Thanks,
Paddy
I don't think that you can achieve that with JUnit.
The only possibility I see is to use an abstract class and inherit that one in every test class.
public abstract class AbstractTest {
#ClassRule
public static ExternalResource resource= new ExternalResource() {
//...
}
}
public class MyTest extends AbstractTest {
}
Also how to ensure that the classes in the suite cannot be run individually?
I don't think that is possible either.
But why would you want that?
After fixing a single failing test, do you really want to re-run all tests to check if one test is working?
(Of course, the fix could have introduced side effects but you can re-run all tests when you know that your fixed test is working.)
Moreover, the tests should be able to run independently.
When you're using the out-of-the-box Runners of JUnit you would get any access from a test Suite to its child tests. Instantiation of the test classes is done internally in the framework.
If you want to pass information/state from one test to another, I'd suppose to implement some kind of "context". E.g. a Singleton with a static Getter and Setter for the context that allows your suite to store an Object and to read it later on from your tests.

How to run test methods in specific order in JUnit4?

I want to execute test methods which are annotated by #Test in specific order.
For example:
public class MyTest {
#Test public void test1(){}
#Test public void test2(){}
}
I want to ensure to run test1() before test2() each time I run MyTest, but I couldn't find annotation like #Test(order=xx).
I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?
I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?
I'm not sure there is a clean way to do this with JUnit, to my knowledge JUnit assumes that all tests can be performed in an arbitrary order. From the FAQ:
How do I use a test fixture?
(...) The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). (...)
Why is it so? Well, I believe that making tests order dependent is a practice that the authors don't want to promote. Tests should be independent, they shouldn't be coupled and violating this will make things harder to maintain, will break the ability to run tests individually (obviously), etc.
That being said, if you really want to go in this direction, consider using TestNG since it supports running tests methods in any arbitrary order natively (and things like specifying that methods depends on groups of methods). Cedric Beust explains how to do this in order of execution of tests in testng.
If you get rid of your existing instance of Junit, and download JUnit 4.11 or greater in the build path, the following code will execute the test methods in the order of their names, sorted in ascending order:
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
#Test
public void testAcreate() {
System.out.println("first");
}
#Test
public void testBupdate() {
System.out.println("second");
}
#Test
public void testCdelete() {
System.out.println("third");
}
}
Migration to TestNG seems the best way, but I see no clear solution here for jUnit. Here is most readable solution / formatting I found for jUnit:
#FixMethodOrder( MethodSorters.NAME_ASCENDING ) // force name ordering
public class SampleTest {
#Test
void stage1_prepareAndTest(){};
#Test
void stage2_checkSomething(){};
#Test
void stage2_checkSomethingElse(){};
#Test
void stage3_thisDependsOnStage2(){};
#Test
void callTimeDoesntMatter(){}
}
This ensures stage2 methods are called after stage1 ones and before stage3 ones.
P.S. I feel this approach is better that jUnit 5.5 #Order annotation because it provides better notation for reader.
If the order is important, you should make the order yourself.
#Test public void test1() { ... }
#Test public void test2() { test1(); ... }
In particular, you should list some or all possible order permutations to test, if necessary.
For example,
void test1();
void test2();
void test3();
#Test
public void testOrder1() { test1(); test3(); }
#Test(expected = Exception.class)
public void testOrder2() { test2(); test3(); test1(); }
#Test(expected = NullPointerException.class)
public void testOrder3() { test3(); test1(); test2(); }
Or, a full test of all permutations:
#Test
public void testAllOrders() {
for (Object[] sample: permute(1, 2, 3)) {
for (Object index: sample) {
switch (((Integer) index).intValue()) {
case 1: test1(); break;
case 2: test2(); break;
case 3: test3(); break;
}
}
}
}
Here, permute() is a simple function which iterates all possible permuations into a Collection of array.
JUnit since 5.5 allows #TestMethodOrder(OrderAnnotation.class) on class and #Order(1) on test-methods.
JUnit old versions allow test methods run ordering using class annotations:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
#FixMethodOrder(MethodSorters.JVM)
#FixMethodOrder(MethodSorters.DEFAULT)
By default test methods are run in alphabetical order. So, to set specific methods order you can name them like:
a_TestWorkUnit_WithCertainState_ShouldDoSomething
b_TestWorkUnit_WithCertainState_ShouldDoSomething
c_TestWorkUnit_WithCertainState_ShouldDoSomething
Or
_1_TestWorkUnit_WithCertainState_ShouldDoSomething
_2_TestWorkUnit_WithCertainState_ShouldDoSomething
_3_TestWorkUnit_WithCertainState_ShouldDoSomething
You can find examples here.
Its one of the main issue which I faced when I worked on Junit and I came up with following solution which works fine for me:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
public class OrderedRunner extends BlockJUnit4ClassRunner {
public OrderedRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
#Override
protected List<FrameworkMethod> computeTestMethods() {
List<FrameworkMethod> list = super.computeTestMethods();
List<FrameworkMethod> copy = new ArrayList<FrameworkMethod>(list);
Collections.sort(copy, new Comparator<FrameworkMethod>() {
#Override
public int compare(FrameworkMethod f1, FrameworkMethod f2) {
Order o1 = f1.getAnnotation(Order.class);
Order o2 = f2.getAnnotation(Order.class);
if (o1 == null || o2 == null) {
return -1;
}
return o1.order() - o2.order();
}
});
return copy;
}
}
also create a interface like below:
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.METHOD})
public #interface Order {
public int order();
}
Now suppose you have class A where you have written several test cases like below:
(#runWith=OrderRunner.class)
Class A{
#Test
#Order(order = 1)
void method(){
//do something
}
}
So execution will start from method named "method()".
Thanks!
JUnit 5 update (and my opinion)
I think it's quite important feature for JUnit, if author of JUnit doesn't want the order feature, why?
By default, unit testing libraries don't try to execute tests in the order that occurs in the source file.
JUnit 5 as JUnit 4 work in that way. Why? Because if the order matters it means that some tests are coupled between them and that is undesirable for unit tests.
So the #Nested feature introduced by JUnit 5 follows the same default approach.
But for integration tests, the order of the test method may matter since a test method may change the state of the application in a way expected by another test method.
For example when you write an integration test for a e-shop checkout processing, the first test method to be executed is registering a client, the second is adding items in the basket and the last one is doing the checkout. If the test runner doesn't respect that order, the test scenario is flawed and will fail.
So in JUnit 5 (from the 5.4 version) you have all the same the possibility to set the execution order by annotating the test class with #TestMethodOrder(OrderAnnotation.class) and by specifying the order with #Order(numericOrderValue) for the methods which the order matters.
For example :
#TestMethodOrder(OrderAnnotation.class)
class FooTest {
#Order(3)
#Test
void checkoutOrder() {
System.out.println("checkoutOrder");
}
#Order(2)
#Test
void addItemsInBasket() {
System.out.println("addItemsInBasket");
}
#Order(1)
#Test
void createUserAndLogin() {
System.out.println("createUserAndLogin");
}
}
Output :
createUserAndLogin
addItemsInBasket
checkoutOrder
By the way, specifying #TestMethodOrder(OrderAnnotation.class) looks like not needed (at least in the 5.4.0 version I tested).
Side note
About the question: is JUnit 5 the best choice to write integration tests? I don't think that it should be the first tool to consider (Cucumber and co may often bring more specific value and features for integration tests) but in some integration test cases, the JUnit framework is enough. So that is a good news that the feature exists.
The (as yet unreleased) change https://github.com/junit-team/junit/pull/386 introduces a #SortMethodsWith. https://github.com/junit-team/junit/pull/293 at least made the order predictable without that (in Java 7 it can be quite random).
Look at a JUnit report. JUnit is already organized by package. Each package has (or can have) TestSuite classes, each of which in turn run multiple TestCases. Each TestCase can have multiple test methods of the form public void test*(), each of which will actually become an instance of the TestCase class to which they belong. Each test method (TestCase instance) has a name and a pass/fail criteria.
What my management requires is the concept of individual TestStep items, each of which reports their own pass/fail criteria. Failure of any test step must not prevent the execution of subsequent test steps.
In the past, test developers in my position organized TestCase classes into packages that correspond to the part(s) of the product under test, created a TestCase class for each test, and made each test method a separate "step" in the test, complete with its own pass/fail criteria in the JUnit output. Each TestCase is a standalone "test", but the individual methods, or test "steps" within the TestCase, must occur in a specific order.
The TestCase methods were the steps of the TestCase, and test designers got a separate pass/fail criterion per test step. Now the test steps are jumbled, and the tests (of course) fail.
For example:
Class testStateChanges extends TestCase
public void testCreateObjectPlacesTheObjectInStateA()
public void testTransitionToStateBAndValidateStateB()
public void testTransitionToStateCAndValidateStateC()
public void testTryToDeleteObjectinStateCAndValidateObjectStillExists()
public void testTransitionToStateAAndValidateStateA()
public void testDeleteObjectInStateAAndObjectDoesNotExist()
public void cleanupIfAnythingWentWrong()
Each test method asserts and reports its own separate pass/fail criteria.
Collapsing this into "one big test method" for the sake of ordering loses the pass/fail criteria granularity of each "step" in the JUnit summary report. ...and that upsets my managers. They are currently demanding another alternative.
Can anyone explain how a JUnit with scrambled test method ordering would support separate pass/fail criteria of each sequential test step, as exemplified above and required by my management?
Regardless of the documentation, I see this as a serious regression in the JUnit framework that is making life difficult for lots of test developers.
Not sure I agree, If I want to test 'File Upload' and then test 'Data Inserted by File Upload' why would I not want these to be independent from each other? Perfectly reasonable I think to be able to run them separately rather than having both in a Goliath test case.
What you want is perfectly reasonable when test cases are being run as a suite.
Unfortunately no time to give a complete solution right now, but have a look at class:
org.junit.runners.Suite
Which allows you to call test cases (from any test class) in a specific order.
These might be used to create functional, integration or system tests.
This leaves your unit tests as they are without specific order (as recommended), whether you run them like that or not, and then re-use the tests as part of a bigger picture.
We re-use/inherit the same code for unit, integration and system tests, sometimes data driven, sometimes commit driven, and sometimes run as a suite.
JUnit 4 update
As of JUnit 4.13 #OrderWith, it is possible to reproduce the JUnit 5 #Order annotation. This solution better integrates with JUnit 4 than #RunWith a custom BlockJUnit4ClassRunner implementation.
Here's how I could replace method name ordering (#FixMethodOrder(MethodSorters.NAME_ASCENDING)) with an ordering by annotation.
#OrderWith(OrderAnnotation.class)
public class MyTest {
#Test
#Order(-1)
public void runBeforeNotAnnotatedTests() {}
#Test
public void notAnnotatedTestHasPriority0() {}
#Test
#Order(1)
public void thisTestHasPriority1() {}
#Test
#Order(2)
public void thisTestHasPriority2() {}
}
/**
* JUnit 4 equivalent of JUnit 5's {#code org.junit.jupiter.api.Order}
*/
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.METHOD })
public #interface Order {
/**
* Default order value for elements not explicitly annotated with {#code #Order}.
*
* #see Order#value
*/
int DEFAULT = 0;
/**
* The order value for the annotated element.
* <p>Elements are ordered based on priority where a lower value has greater
* priority than a higher value. For example, {#link Integer#MAX_VALUE} has
* the lowest priority.
*
* #see #DEFAULT
*/
int value();
}
import org.junit.runner.Description;
import org.junit.runner.manipulation.Ordering;
import org.junit.runner.manipulation.Sorter;
/**
* Order test methods by their {#link Order} annotation. The lower value has the highest priority.
* The tests that are not annotated get the default value {#link Order#DEFAULT}.
*/
public class OrderAnnotation extends Sorter implements Ordering.Factory {
public OrderAnnotation() {
super(COMPARATOR);
}
#Override
public Ordering create(Context context) {
return this;
}
private static final Comparator<Description> COMPARATOR = Comparator.comparingInt(
description -> Optional.ofNullable(description.getAnnotation(Order.class))
.map(Order::value)
.orElse(Order.DEFAULT));
}
The not annotated tests get a default priority of 0. The order of tests with the same priority is undetermined.
Gist: https://gist.github.com/jcarsique/df98e0bad9e88e8258c4ab34dad3c863
Inspired by:
Aman Goel's answer
Test execution order Wiki by JUnit team
JUnit 5 source code
See my solution here:
"Junit and java 7."
In this article I describe how to run junit tests in order - "just as in your source code".
Tests will be run, in order as your test methods appears in class file.
http://intellijava.blogspot.com/2012/05/junit-and-java-7.html
But as Pascal Thivent said, this is not a good practise.
As others have stated, tests should be ideally be independent of execution order. This makes tests less fragile, and allows them to be run independently (many IDEs allow you to select a test method and execute it independently of other tests).
That being said, for integration tests, some people prefer to rely on method ordering.
Starting with JUnit 4.13 you can define your own class to reorder tests by extending Ordering. See the JUnit wiki for more details. Here's an example using the built-in Alphanumeric class to order the tests alphanumerically using the test method name:
import org.junit.Test;
import org.junit.runner.OrderWith;
import org.junit.runner.manipulation.Alphanumeric;
#OrderWith(Alphanumeric.class)
public class TestMethodOrder {
#Test
public void testA() {
System.out.println("first");
}
#Test
public void testB() {
System.out.println("second");
}
#Test
public void testC() {
System.out.println("third");
}
}
For JUnit 4, putting this annotation on the test class solved the problem.
#FixMethodOrder(MethodSorters.JVM)
With JUnit 5.4, you can specify the order :
#Test
#Order(2)
public void sendEmailTestGmail() throws MessagingException {
you just need to annotate your class
#TestMethodOrder(OrderAnnotation.class)
https://junit.org/junit5/docs/current/user-guide/#writing-tests-test-execution-order
i'm using this in my project and it works very well !
You can use one of these piece of codes:
#FixMethodOrder(MethodSorters.JVM) OR #FixMethodOrder(MethodSorters.DEFAULT) OR #FixMethodOrder(MethodSorters.NAME_ASCENDING)
Before your test class like this:
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BookTest {...}
It's time to move to Junit5.
Here is a sample of what we could get:
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class OrderedTests {
#Test
#Order(1)
void nullValues() {}
#Test
#Order(2)
void emptyValues() {}
#Test
#Order(3)
void validValues() {}
}
For Junit4, copy the logic that you have in several tests in one test method.
I've read a few answers and agree its not best practice, but the easiest way to order your tests - and the way that JUnit runs tests by default is by alphabetic name ascending.
So just name your tests in the alphabetic order that you want. Also note the test name must begin
with the word test. Just watch out for numbers
test12 will run before test2
so:
testA_MyFirstTest
testC_ThirdTest
testB_ATestThatRunsSecond
Please check out this one: https://github.com/TransparentMarket/junit. It runs the test in the order they are specified (defined within the compiled class file). Also it features a AllTests suite to run tests defined by sub package first. Using the AllTests implementation one can extend the solution in also filtering for properties (we used to use #Fast annotations but those were not published yet).
Here is an extension to JUnit that can produce the desired behavior: https://github.com/aafuks/aaf-junit
I know that this is against the authors of JUnit philosophy, but when using JUnit in environments that are not strict unit testing (as practiced in Java) this can be very helpful.
I ended up here thinking that my tests weren't run in order, but the truth is that the mess was in my async jobs. When working with concurrency you need to perform concurrency checks between your tests as well.
In my case, jobs and tests share a semaphore, so next tests hang until the running job releases the lock.
I know this is not fully related to this question, but maybe could help targeting the correct issue
If you want to run test methods in a specific order in JUnit 5, you can use the below code.
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MyClassTest {
#Test
#Order(1)
public void test1() {}
#Test
#Order(2)
public void test2() {}
}

JUnit TestCase object instantiation

Is a new (or different) instance of TestCase object is used to run each test method in a JUnit test case? Or one instance is reused for all the tests?
public class MyTest extends TestCase {
public void testSomething() { ... }
public void testSomethingElse() { ... }
}
While running this test, how many instances of MyTest class is created?
If possible, provide a link to a document or source code where I can verify the behaviour.
Yes, a separate instance is created.
While running that test, 2 instances of MyTest gets created.
If you want a different behavior, one option is to use a similar tool called TestNG(http://testng.org/doc/).
I couldn't find a clear answer in the JUnit docs about your question, but the intent, as anjanb wrote, is that each test is independent of the others, so a new TestCase instance could be created for each test to be run.
If you have expensive test setup ("fixtures") that you want to be shared across all test cases in a test class, you can use the #BeforeClass annotation on a static method to achieve this result: http://junit.sourceforge.net/javadoc_40/org/junit/BeforeClass.html. Note however, that a new instance may still be created for each test, but that won't affect the static data your #BeforeTest method has initialized.
There's one instance for each test run. Try
public class MyTest extends TestCase {
public MyTest() { System.out.println("MyTest Constructor");
public void setUp() { System.out.println("MyTest setUp");
public void tearDown() { System.out.println("MyTest tearDown");
public void testSomething() { System.out.println("MyTest testSomething");
public void testSomethingElse() { System.out.println("MyTest testSomethingElse");
}
The Sourcecode (including that to newer versions - your and my example is Junit 3) is on http://www.junit.org
If you are asking this because you are concerned about data being initialized and re-initialized in your constructor, be aware that the prescribed way to initialize your test cases data is via setUp() and tearDown() exclusively.
Yes, definitely. I found that data I stored in instance variables could not be accessed between tests due to this design.

Categories