JUnit TEAM SEGFAULT ARJUN BHASIN CHAKORI MACHERLA GUNJAN RAGHAV JAIDEEP SINGH VICKY SEHRAWAT What is JUnit? JUnit is a regression testing framework Used for unit testing framework in Java programs User interface for viewing test results Text GUI Integrated with several IDEs like Eclipse It is one of a family of unit testing frameworks collectively known as xUnit that originated with SUnit. Who developed JUnit? Written by Erich Gamma and Kent Beck. Used by developers to implement unit tests in Java Why JUnit? Test framework provides tools for: Assertions Running tests Aggregating Tests (suites) Reporting Results Less reliance on time consuming debuggers JUnit improves our code quality Features! JUnit features include: Assertions for testing expected results Test fixtures for sharing common test data Test suites for easily organizing and running tests Graphical and textual test runners Limitations Impossible to test a client/server environment, or even a peer-to-peer scenario. We cannot perform distributed tests, so you don’t have a way to run the same series of tests simultaneously on several JVMs each running on different Operating Systems. A Quick Demo!! Setup and Teardown Setup: Use the @Before annotation on a method containing code to run before each test case. Teardown: Use the @After annotation on a method containing code to run after each test case. These methods will run even if exceptions are thrown in the test case or an assertion fails. It is allowed to have any number of these annotations. All methods annotated with @Before will be run before each test case, but they may be run in any order. Example: Using a file as a text fixture public class OutputTest { private File output; @Before public void createOutputFile() { output = new File(...); } @After public void deleteOutputFile() { output.delete(); } @Test public void test1WithFile() { // code for test case objective } } @Test public void test2WithFile() { // code for test case objective } Method execution order 1. createOutputFile() 2. test1WithFile() 3. deleteOutputFile() 4. createOutputFile() 5. test2WithFile() 6. deleteOutputFile() Assumption: test1WithFile runs before test2WithFile. Adding Tests to TestCases Any method in a TestCase class is considered a test if it begins with the word test You can write many tests (have many test methods) Each test method should use a variety of assert methods to test things about the state of their classes under tests Assert methods are inherited Assert Methods Assert methods include: assertEqual(x,y) assertFalse(boolean) assertTrue(boolean) assertNull(object) assertNotNull(object) assetSame(firstObject, secondObject) assertNotSame(firstObject, secondObject) Adding Two Tests to TestCase package testing; import junit.framework.TestCase; public class FirstTestCase extends TestCase { public FirstTestCase(String arg0) { super(arg0); } public static void main(String[] args) {} protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } public void testCompareSucceed() { assertEquals(0, 0); //this assertion will succeed } public void testCompareFail() { assertEquals(0, 1); //this assertion will fail } } Sources https://staff.ti.bfh.ch/fileadmin/home/due1/uml_d p/JUnitTutorial-Williams-adapted.pdf http://vishnuagrawal.wordpress.com/2007/04/30/l imitation-of-junit/ http://en.wikipedia.org/wiki/JUnit http://www.junit.org/ Questions