Advertisement
Guest User

Junit_TestClass_Version_2

a guest
Oct 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import static org.junit.Assert.assertEquals;
  2. import static org.junit.Assert.fail;
  3.  
  4. import org.junit.After;
  5. import org.junit.AfterClass;
  6. import org.junit.Before;
  7. import org.junit.BeforeClass;
  8. import org.junit.Ignore;
  9. import org.junit.Test;
  10.  
  11. public class CalculatorTest {
  12.  
  13. private static Calculator calculator;
  14.  
  15. @BeforeClass
  16. public static void setUpClass() throws Exception {
  17. calculator = new Calculator();
  18. }
  19.  
  20. @AfterClass
  21. public static void tearDownClass() throws Exception {
  22. calculator.switchOff();
  23. }
  24.  
  25. @Before
  26. public void setUp() throws Exception {
  27. calculator.clear();
  28. }
  29.  
  30. @After
  31. public void tearDown() throws Exception {
  32. calculator.clear();
  33. }
  34.  
  35. /**
  36. * Test of add method, of class Calculator.
  37. */
  38. @Test
  39. public void testAdd() {
  40. System.out.println("add");
  41. int n = 0;
  42. calculator.set(0);
  43. calculator.add(5);
  44. assertEquals(5, calculator.getResult());
  45.  
  46. }
  47.  
  48. /**
  49. * Test of substract method, of class Calculator.
  50. */
  51. @Test
  52. public void testSubstract() {
  53. System.out.println("substract");
  54. int n = 3;
  55. calculator.set(5);
  56. calculator.substract(n);
  57. assertEquals(2, calculator.getResult());
  58. }
  59.  
  60. /**
  61. * Test of multiply method, of class Calculator.
  62. */
  63. @Test
  64. public void testMultiply() {
  65. System.out.println("multiply");
  66. int n = 2;
  67. calculator.set(4);
  68. calculator.multiply(n);
  69. assertEquals(8, calculator.getResult());
  70.  
  71. }
  72.  
  73. /**
  74. * Test of divide method, of class Calculator.
  75. */
  76. @Test(/* expected= ArithmeticException.class */)
  77. public void testDivide() {
  78. System.out.println("divide");
  79. int n = 0;
  80. calculator.set(10);
  81. calculator.divide(n);
  82.  
  83. }
  84.  
  85. /**
  86. * Test of square method, of class Calculator.
  87. */
  88. @Test
  89. public void testSquare() {
  90. System.out.println("square");
  91. int n = 0;
  92. calculator.square(n);
  93.  
  94. }
  95.  
  96. /**
  97. * Test of squareRoot method, of class Calculator.
  98. */
  99. @Test
  100. public void testSquareRoot() {
  101. System.out.println("squareRoot");
  102. int n = 0;
  103. calculator.set(0);
  104. calculator.squareRoot(n);
  105.  
  106. }
  107.  
  108. /**
  109. * Test of clear method, of class Calculator.
  110. */
  111. @Test
  112. // @Ignore
  113. public void testClear() {
  114. System.out.println("clear");
  115. Calculator instance = new Calculator();
  116. instance.clear();
  117. // TODO review the generated test code and remove the default call to
  118. // fail.
  119. fail("The test case is a prototype.");
  120. }
  121.  
  122. /**
  123. * Test of switchOn method, of class Calculator.
  124. */
  125. @Test
  126. public void testSwitchOn() {
  127. System.out.println("switchOn");
  128. calculator.switchOn();
  129.  
  130. }
  131.  
  132. /**
  133. * Test of switchOff method, of class Calculator.
  134. */
  135. @Test
  136. public void testSwitchOff() {
  137. System.out.println("switchOff");
  138. calculator.switchOff();
  139.  
  140. }
  141.  
  142. /**
  143. * Test of getResult method, of class Calculator.
  144. */
  145. @Test
  146. public void testGetResult() {
  147. System.out.println("getResult");
  148. int expResult = 0;
  149. int result = calculator.getResult();
  150. assertEquals(expResult, result);
  151. }
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement