- JUnit - Extensions
- JUnit - Plug with Eclipse
- JUnit - Plug with Ant
- JUnit - Parameterized Test
- JUnit - Exceptions Test
- JUnit - Time Test
- JUnit - Ignore Test
- JUnit - Suite Test
- JUnit - Executing Tests
- JUnit - Execution Procedure
- JUnit - Using Assertion
- JUnit - Writing a Tests
- JUnit - API
- JUnit - Basic Usage
- JUnit - Test Framework
- JUnit - Environment Setup
- JUnit - Overview
- JUnit - Home
JUnit Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JUnit - Suite Test
Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests. This chapter takes an example having two test classes, TestJunit1 & TestJunit2, that run together using Test Suite.
Create a Class
Create a java class to be tested, say, MessageUtil.java in C:>JUNIT_WORKSPACE.
/* * This class prints the given message on console. */ pubpc class MessageUtil { private String message; //Constructor //@param message to be printed pubpc MessageUtil(String message){ this.message = message; } // prints the message pubpc String printMessage(){ System.out.println(message); return message; } // add "Hi!" to the message pubpc String salutationMessage(){ message = "Hi!" + message; System.out.println(message); return message; } }
Create Test Case Classes
Create a java class file named TestJunit1.java in C:>JUNIT_WORKSPACE.
import org.junit.Test; import org.junit.Ignore; import static org.junit.Assert.assertEquals; pubpc class TestJunit1 { String message = "Robert"; MessageUtil messageUtil = new MessageUtil(message); @Test pubpc void testPrintMessage() { System.out.println("Inside testPrintMessage()"); assertEquals(message, messageUtil.printMessage()); } }
Create a java class file named TestJunit2.java in C:>JUNIT_WORKSPACE.
import org.junit.Test; import org.junit.Ignore; import static org.junit.Assert.assertEquals; pubpc class TestJunit2 { String message = "Robert"; MessageUtil messageUtil = new MessageUtil(message); @Test pubpc void testSalutationMessage() { System.out.println("Inside testSalutationMessage()"); message = "Hi!" + "Robert"; assertEquals(message,messageUtil.salutationMessage()); } }
Create Test Suite Class
Create a java class.
Attach @RunWith(Suite.class) Annotation with the class.
Add reference to JUnit test classes using @Suite.SuiteClasses annotation.
Create a java class file named TestSuite.java in C:>JUNIT_WORKSPACE to execute test case(s).
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ TestJunit1.class, TestJunit2.class }) pubpc class JunitTestSuite { }
Create Test Runner Class
Create a java class file named TestRunner.java in C:>JUNIT_WORKSPACE to execute test case(s).
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; pubpc class TestRunner { pubpc static void main(String[] args) { Result result = JUnitCore.runClasses(JunitTestSuite.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
Compile all the java classes using javac.
C:JUNIT_WORKSPACE>javac MessageUtil.java TestJunit1.java TestJunit2.java JunitTestSuite.java TestRunner.java
Now run the Test Runner, which will run the test case defined in the provided Test Case class.
C:JUNIT_WORKSPACE>java TestRunner
Verify the output.
Inside testPrintMessage() Robert Inside testSalutationMessage() Hi Robert trueAdvertisements