In JUnit, you can run multiple test cases with @RunWith and @Suite annotation. Refer to the following examples :
SuiteAbcTest.java
package com.mkyong;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Exception1Test.class, //test case 1
TimeoutTest.class //test case 2
})
public class SuiteAbcTest {
//normally, this is an empty class
}
P.S Tested with JUnit 4.12
Hi Mkyong,
I have one requirement regarding parallel unit test cases in Integration testing. I will explain my requirement. Request you to kindly help me out for solution.
Problem Statement :
I am having 6 Junit classes and each class is having “n” number of test methods.Each test method will contact one service call.This service will create unique order and sends unique orderid to test method. After that those services will process the orders in backend(this is service task not my test method’s task). So existing all test cases are doing that. Now my updated requirement is, Need to track the order status Parallely. I mean each method should execute and give the test result and parallely this test mehtod output should pass to another common method which will take the input as order Id and check the order status and then based on status value this common method should do assertEqual.
So all test method execution flow will not stop untill the common method execution response. Please suggest.
Hi can we get Successful test result in Junit Test Suite
Hi Mkyong,
I am trying to write a test suit using groovy but it is saying their is error in the code
package TestSuits;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
Exception1Test.class //test case 1
TimeoutTest.class //test case 2
})
public class SuiteAbcTest {
//normally, this is an empty class
}
Error Message:
(gotten on line 8)
Annotation List attribute must use groovy notation [el1,el2] in @org.junit.runners.Suite$SuiteClasses
Let’s assume i’ve got Test inside JunitTest1.class that fails for any reason. How to pass back error message to suite e.g like:
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: ‘2.46.0’, revision: ‘61506a4624b13675f24581e453592342b7485d71’, time: ‘2015-06-04 10:22:50’
System info: host: ‘STI-WR-029’, ip: ‘172.16.20.99’, os.name: ‘Windows 8.1’, os.arch: ‘amd64’, os.version: ‘6.3’, java.version: ‘1.8.0_40’
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:269)
at org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:114)
at containers.TestMethods.search(TestMethods.java:1050)
at containers.TestMethods.search(TestMethods.java:1041)
at tests.SearchAndAutoComplete.checkArtistAliases(SearchAndAutoComplete.java:265)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:155)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:284)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:143)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:89)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:134)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:168)
at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:392)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:578)
… 52 more
hi
i have few doubts, could you please clarify
in Step3 : Create Test Suite Class : – how can i get the details of junit test classes executed( pass/fail, test case execution start time, end time etc..) from this suite class ?
in Step4 : Create Test Runner : How Can i call/execute this class from Ant Build File or Jenkins server ?
I am trying to generate a custom html report file for the Junit Test Suite, so for that i want to know above details.
Oh no!!! Where is your eyes friend????!!!
realyy nice explaination of JUNIT test with example
HI GUYS ,I am Providing you the complete Solution for this : KEEP CODING
Note : Just Create hierarchy :
E:.
? .classpath
? .project
? projectSet.psf
?
????.settings
? org.eclipse.jdt.core.prefs
?
????bin
? ????com
? ????cxt
? ????java
? ????source
? ? Bank.class
? ?
? ????test
? BankTest.class
? BankTestSuite.clas
? MyTestRunner.class
?
????src
????com
????cxt
????java
????source
? Bank.java
?
????test
BankTest.java
BankTestSuite.java
MyTestRunner.java
—————————————————————————————
**STEP 1 : CREATE THE SOURCE PROGRAM.**
package com.cxt.java.source; public class Bank { public long accno; public double amount; public double withdraw; static double security_amount=1000.00; public double depositeMoney(long accountno, double amount) { this.accno=accountno; this.amount=amount; this.amount+=amount; System.out.println("Amount "+amount+" has been successfully deposited into A/C Number : "+accountno); System.out.println("Available Balence : "+this.amount+" & Security Deposite : "+security_amount); return amount; } public double withdrawMoney(long accountno, double amount) { this.accno=accountno; this.amount=amount; this.amount-=amount; if (this.amount= amount) { System.out.println("Withdrawing form the Security Deposite Ammount"); this.security_amount-=amount; System.out.println("Amount $ "+amount+" has been successfully withdrawed from A/C Number : "+accountno); } else { System.out.println("The enterd amount is bigger than the available as & security deposited amount.\n Hence Can't be withdraw."); System.out.println("Amount $ 00.00 has been successfully withdrawed from A/C Number : "+accountno); } System.out.println("Available Balence : "+this.amount+" & Security Deposite : "+security_amount); return this.amount; } public double checkBalence() { System.out.println("Current A/C Balence is :"+this.amount+" in "+this.accno+" A/C number"); return this.amount; } }**STEP 2 : CREATE TEST CLASS.**
package com.cxt.java.test; import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import com.cxt.java.source.Bank; public class BankTest { static Bank bank; @BeforeClass public static void setUpBeforeClass() throws Exception { if (bank==null) { bank=new Bank(); } else { System.out.println("Bank object should be singleton in nature.."); } } @AfterClass public static void tearDownAfterClass() throws Exception { bank=null; System.gc(); } @Before public void setUp() throws Exception { System.out.println("setUp()"); } @After public void tearDown() throws Throwable { System.out.println("tearDown()\n"); //System.gc(); } @Test public final void testDepositeMoney() { long accno=1001; double amount=1000.00; double expected=amount; double actual=bank.depositeMoney(accno, amount); assertEquals("Exception raised in testDepositeMoney()",expected,actual,0.0); } @Test public final void testWithdrawMoney() { long accno=1000; double amount=1000.00; double expected=0.0; double actual=bank.withdrawMoney(accno, amount); assertEquals("Exception Raised into testWithdrawMoey()",expected, actual,0.0); } @Test public final void testWithdrawAccessMoney() { long accno=1001; double amount=100000.00; double expected=0.0; double actual=bank.withdrawMoney(accno, amount); assertEquals("Exception Raised into testWithdrawMoey()",expected, actual,0.0); } }**STEP 3 : CREATE TEST SUITE CLASS**
package com.cxt.java.test; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; import junit.framework.TestResult; import junit.framework.TestSuite; @RunWith(Suite.class) @SuiteClasses({ BankTest.class }) public class BankTestSuite { }**STEP 4 : CREATE TEST RUNNER**
package com.cxt.java.test; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class MyTestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(BankTestSuite.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println("Was Successful.?"+result.wasSuccessful()); System.out.println("Total Run Count : "+result.getRunCount()); System.out.println();System.out.println();System.out.println(); System.out.pritnln("... Jay Maharastra ..."); } }Hi MK Yong …
Iam big fan of you. Recently i started reading your posted on various technologies, it is really help for others.
I have a doubt on JUNIT Testcases. Please provide the information to that
public class Emp
{
public void insertEmp(Connection,String sql)
{
// Logic to perform conn and insert an employee object into the database
}
}
So, i want to perform a testcase to the above Emp class insertEmp(-,-), but the return type of insertEmp(-,-) is void . so How should we need to write testcase for that insert(-,-)
ASP please give the solution..
Thank you…………
PLEASE HELP TO SOLVE THE ISSUE. ============================================ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package junit1; /** * * @author Shrikant.Kale */ public class JUNIT1 { public int add(int x, int y) { return x+y; } public int sub(int x, int y) { return x-y; } public int mult(int x, int y) { return x*y; } } ------------------------------------------------ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package junit1; import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; /** * * @author Shrikant.Kale */ public class JUNIT1Test { JUNIT1 test1; @Before public void setUp() { System.out.println("startUp()"); test1=new JUNIT1(); } @After public void tearDown() { System.out.println("tearDown()"); System.out.println(); test1=null; } @Test public void testAdd() { System.out.println("Testing add()"); assertEquals(10,test1.add(5, 5)); } /** * Test of sub method, of class JUNIT1. */ @Test public void testSub() { System.out.println("Testing sub()"); assertEquals(10,test1.sub(20,10)); } /** * Test of mult method, of class JUNIT1. */ @Test public void testMult() { System.out.println("Testing mult()"); assertEquals(100,test1.mult(10,10)); } } ---------------------------------------------- /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package junit1; import junit.framework.TestResult; import junit.framework.TestSuite; /** * * @author Shrikant.Kale */ public class JUnitTestSUITE { public static void main(String[] args) { TestSuite suite= new TestSuite(JUNIT1Test.class); TestResult result=new TestResult(); suite.run(result); System.out.println("Total Run Test : "+result.runCount()); System.out.println("Was Successful : "+result.wasSuccessful()); } } ============================================ run: Total Run Test : 1 Was Successful : false BUILD SUCCESSFUL (total time: 0 seconds) ........................................................................does the @RunWith(Suite.class) always run the classes in @Suite.SuiteClasses() in the order they are specified?
Hi. Is there any way to use a class object initialized in first Test method to be used in all other Test methods in the same class. Like
public class TestCl extends TestCase { Student stud; Fees fee; @Test public void create() { fee=new Fee(); fee.setAmt(10); feeService.save(fee); stud=new Student(); stud.setName("Monika"); stud.setFees(fee); studService.save(stud); } @Test public void testFeeByStud() { Fee feeFound= studService.findFeeByStud(stud); //Here stud is always null. Can we get the stud initializes in previous method. Assert.assertNotNull(feeFound); } }use setup();
Thanks for your all example and support. It was really helpful to understand in quick time.
Nice article, depicting the topics effectively.
keep up the work.
rastogi
Consultants available for Software testing, Test Management requirements. reach us at Testing-Associates
Hi, can i use @Runwith(Suite.class) Next to @SpringBootTest?
Hi Mkyong,
All examples I found about SuiteClass has 2 or 3 actual tests in them.
It may be convenient for a quick tutorial, but how do you handle hundreds of tests in a test suite ? Do you add them all one by one in @SuiteClass() or can you use some agregation to automatically select them, all or groups of them.
Thanks
All good… I found this library that allows to use wildcards to select any test in your project.
https://github.com/michaeltamm/junit-toolbox
package examples;
import org.junit.runner.RunWith;
//import org.junit.runners.Suite;
//import org.junit.runners.Suite.SuiteClasses;
import com.googlecode.junittoolbox.WildcardPatternSuite;
import com.googlecode.junittoolbox.SuiteClasses;
//@RunWith(Suite.class)
//@SuiteClasses({TestA.class, TestB.class, TestC.class})
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({“**/*.class”}) // Select all test classes recursively
public class TestSuite {
}