package com.junit.suite.example; public class Math { public static int addition(int num1, int num2) { return num1 + num2; } public static int substraction(int num1, int num2) { return num1 - num2; } public static double division(int num1, int num2) { return num1 / num2; } }
This is test class for Addition
package com.junit.suite.example; import static org.junit.Assert.*; import java.util.Arrays; import java.util.List; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; // perform the test using parameters @RunWith(Parameterized.class) public class AdditionMathTest { int expectedNumber; int num1; int num2; // create a constructor to pass the values public AdditionMathTest(int expectedNumber, int num1, int num2) { // TODO Auto-generated constructor stub this.expectedNumber = expectedNumber; this.num1 = num1; this.num2 = num2; } // create a parameterized method @Parameters public static List<Object[]> createData() { return Arrays.asList(new Object[][] { {4,2,1}, {6,3,3}, {8,4,4} }); } @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testAddition1() { // System.out.println(expectedNumber+"\t"+num1+"\t"+num2); Assert.assertTrue(expectedNumber==(Math.addition(num1, num2))); } @Test public void testAddition2() { Assert.assertEquals(expectedNumber, Math.addition(num1, num2)); } }
This is test class for Substraction
package com.junit.suite.example; import static org.junit.Assert.*; import java.util.Arrays; import java.util.List; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class SubstractionMathTest { int expectedNumber; int num1; int num2; // create a constructor to pass the values public SubstractionMathTest(int expectedNumber, int num1, int num2) { // TODO Auto-generated constructor stub this.expectedNumber = expectedNumber; this.num1 = num1; this.num2 = num2; } // create a parameterized method @Parameters public static List<Object[]> createData() { return Arrays.asList(new Object[][] { { 4, 2, 1 }, { 6, 3, 3 }, { 8, 4, 4 } }); } @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testAddition() { // System.out.println(expectedNumber+"\t"+num1+"\t"+num2); Assert.assertTrue(expectedNumber == (Math.substraction(num1, num2))); } @Test public void testAddition2() { Assert.assertEquals(expectedNumber, Math.substraction(num1, num2)); } }
This is test class for Division
package com.junit.suite.example; import static org.junit.Assert.*; import java.util.Arrays; import java.util.List; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class DivisionMathTest { int expectedNumber; int num1; int num2; // create a constructor to pass the values public DivisionMathTest(int expectedNumber, int num1, int num2) { // TODO Auto-generated constructor stub this.expectedNumber = expectedNumber; this.num1 = num1; this.num2 = num2; } // create a parameterized method @Parameters public static List<Object[]> createData() { return Arrays.asList(new Object[][] { { 4, 2, 1 }, { 6, 3, 3 }, { 8, 4, 4 } }); } @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testAddition() { // System.out.println(expectedNumber+"\t"+num1+"\t"+num2); Assert.assertTrue(expectedNumber == (Math.division(num1, num2))); } }
This is where the suite is implemented. All test classes are clubbed together
package com.junit.suite.example; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ AdditionMathTest.class, SubstractionMathTest.class, DivisionMathTest.class }) public class MathTestSuite { }
This is the runner class for suite
package com.junit.suite.example; import java.util.Iterator; import java.util.List; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class MathTestRunner { public static void main(String[] args) { Result run = JUnitCore.runClasses(MathTestSuite.class); List<Failure> failures = run.getFailures(); Iterator<Failure> it = failures.iterator(); while (it.hasNext()) { System.out.println(it.next().toString()); } } }
HHH
No comments:
Post a Comment