Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have two individual test classes ( for example : TestClassA, TestClassB) I want TestClassA to run first and TestClassB to run next. How can i do so. Any ideas.
Note that i am using Springboot here and my test dependency is : spring-boot-starter-test
you can do using junit 5
#Test
#Order(1)
public void firstTest() {
output.append("a");
}
#Test
#Order(2)
public void secondTest() {
output.append("b");
}
check refence here https://www.baeldung.com/junit-5-test-order
and for different classes use test suite
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 {
}
As mentioned in the comments, ordering tests or having tests dependent on each other is an anti pattern and make your testing strategy brittle. So ordering should be handled with care.
With Junit 4 you can order your test
#TestMethodOrder(OrderAnnotation.class)
public class OrderedTest {
#Test
#Order(1)
public void firstTest() {
}
#Test
#Order(2)
public void secondTest() {
}
#Test
#Order(3)
public void thirdTest() {
}
}
With Junit 5
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MethodOrderTest {
#Test
#Order(3)
void test1() {
}
#Test
#Order(1)
void test2() {
}
#Test
#Order(2)
void test3() {
}
}
Related
I have a class with list of tests annotated with #Test. I commented #Test annotation for one of the test however when I am running the suit the commented test is also running. Any idea on what's happening?
Example:
public class TestCasesClass {
#BeforeSuite
public void testSetup() throws Exception
{
super.testSetup();
}
#Test
public void test1() {
//Some test code
}
#Test
public void test12() {
//Some test code
}
//#Test
public void test3() {
//Some test code
}
}
When running the test suit all the test are running, including test3.
I tried #Test(enable-false) then also test running in the suit.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
As I googled, this question have accepted answer saying, that there is a way to implement the #ClassRule by using AfterXXX and BeforeXXX methods.
How to implement #ClassRule using those methods?
JUnit:
public class UsesExternalResource {
public static Server myServer= new Server();
#ClassRule
public static ExternalResource resource= new ExternalResource() {
#Override
protected void before() throws Throwable {
myServer.connect();
};
#Override
protected void after() {
myServer.disconnect();
};
};
}
TestNG:
public class UsesExternalResource {
public Server myServer= new Server();
#BeforeClass
public void before() {
myServer.connect();
}
#AfterClass
public void before() {
myServer.disconnect();
}
}
Here's my code below, testSample() gets executed successfully. Please suggest what could possibly be wrong
class DataServiceTest extends GrailsUnitTestCase{
#BeforeClass
static void onceExecutedBeforeAll() {
println(" Print before Start Test Cases");
}
#Test
public void testSample(){
println(" Inside Sample");
}
}
You can't extend a TestCase and use annotations at the same time. If you want to create a test suite with annotations, you can use #RunWith annotation:
#RunWith(Suite.class)
#Suite.SuiteClasses({ DataServiceTest.class, OtherTest.class })
public class AllTests {
// empty
}
public class DataServiceTest { // no extends here
#BeforeClass
static void onceExecutedBeforeAll() {
println(" Print before Start Test Cases");
}
#Test
public void testSample(){
println(" Inside Sample");
}
}
Another option using JUnit could be annotating the method with #Before and removing extends GrailsUnitTestCase from the class.
I am using a RunListener to let test fail when they write to System.out, but when I fail() a unittest, the listener is removed. Is there any way to let tests fail without removing the Listener?
For clarification a code example
public class OutputListenerTest {
#Test
public void testPrintIsDicovered() {
JUnitCore runner = new JUnitCore();
// the OutputListener calls fail() when something was written
runner.addListener(new OutputListener());
Result result = runner.run(TestWithOutput.class);
}
public static class TestWithOutput {
#Test
public void testOutput1() {
System.out.println("foo");
}
#Test
public void testOutput2() {
System.out.println("bar");
}
}
}
What I'd expect: 2 failed tests
What is: The first test fails and the Listener is removed.
As requested, here is the OutputListener code http://paste.robbi5.com/4916ca5b
Is it ok to not paste it here, it's pretty long and won't help solving the question?
a little more context
I picked the RunListener, because it works pretty easy with maven, just add
<properties>
<property>
<name>listener</name>
<value>OutputListener</value>
</property>
</properties>
to the maven-surefire-plugin and mvn test shows what tests use System.out in some way.
Add a Runner to add the listener.
public class AddListenerRunner extends BlockJUnit4ClassRunner {
public AddListenerRunner(Class<?> klass) throws InitializationError {
super(klass);
}
#Override public void run(RunNotifier notifier){
notifier.addListener(new OutputListener());
super.run(notifier);
}
}
You can then use that in your tests like this.
#RunWith(AddListenerRunner.class)
public class OutputListenerTest {
#Test
public void testOutput1() {
System.out.println("foo");
}
#Test
public void testOutput2() {
System.out.println("bar");
}
}
I am trying to use the Junit4 i using the below clases
public class Example extends TestCase {
public Example(String name) {
super(name);
}
#Test
public void exampletest() {
//TO DO
}
}
#RunWith(Suite.class)
#SuiteClasses({ Example.class })
public class Tests {
TestSuite tests = new TestSuite();
tests.addTest(new Example("exampletest"));
}
It gives me No tests found in junit4 exception some one can tell me why i get this exception
Or give an example how to do it?
In JUnit4, you don't make your test cases extend TestCase. But if you do, then your #Test annotations are ignored, and you have to prepend test method names by test.
Try this code:
Example.java:
import org.junit.Test;
public class Example {
#Test
public void exampletest() {
//TO DO
}
}
Tests.java:
#RunWith(Suite.class)
#SuiteClasses({ Example.class })
public class Tests {
}