Пример #1
0
  private void testAdd() {
    // Simple test, does it work with 2 operands.
    System.out.print("Test: Adding two strings");
    int expected = 2;
    if (calculator.add("1,1") == expected) {
      System.out.println("...passed!");
    } else {
      System.out.println("...failed");
    }

    // Simple test, does it throw an exception for one or no operands?
    System.out.print("Test: Throws IllegalArgumentException if no operands.");
    try {
      calculator.add("1");
      System.out.println("...failed!");
    } catch (IllegalArgumentException ex) {
      System.out.println("...passed!");
    }

    // Simple test, does it throw an exception if an operand isn't a number?
    System.out.print("Test: Throws NumberFormatException if operand isn't a number.");
    try {
      calculator.add("1, a");
      System.out.println("...failed!");
    } catch (NumberFormatException ex) {
      System.out.println("...passed!");
    }
  }
 @Test
 public void addWithNegatives() {
   StringCalculator sc = new StringCalculator();
   try {
     sc.add("//x\n2x3x-4\n5x6x7x8");
     fail("An exception must have been thrown");
   } catch (RuntimeException e) {
     assertNotNull(e);
     assertEquals("Negative not allowed", e.getMessage());
   }
 }
  @TestWith({"1 - 1", "2 - 2"})
  public void add_singleNumber_ReturnsIt(int expected, String expression) throws Exception {

    int actual = calculator.add(expression);

    assertEquals(expected, actual);
  }
  @Test
  public void add_ManyNumbers_ReturnsTheSum() throws Exception {

    int actual = calculator.add("1,2,3,4,5,6");

    assertEquals(21, actual);
  }
  @TestWith({"2 - 1,1", "3 - 1,2"})
  public void add_TwoNumbers_ReturnsTheSum(int expected, String expression) throws Exception {

    int actual = calculator.add(expression);

    assertEquals(expected, actual);
  }
  @Test
  public void add_emptyInput_ReturnsZero() throws Exception {

    int actual = calculator.add("");

    assertEquals(0, actual);
  }
 @Test
 public void shouldShowNegativeNumbersInExceptionMessage() {
   try {
     stringCalculator.add("-1,2,-3");
   } catch (RuntimeException exc) {
     assertThat(exc.getMessage(), is("Negatives not allowed: -1 -3 "));
   }
 }
 @Test
 public void shouldThrowExceptionForNegativeNumbers() {
   try {
     stringCalculator.add("-1");
     fail("RuntimeException expected.");
   } catch (RuntimeException exc) {
     // test passes
   }
 }
 @Test
 public void add_NegativeNumbers_ThrowsExceptionWithMessageContainingAllNegativeNumbers()
     throws Exception {
   try {
     calculator.add("-1,-2,-3");
     fail("Should have thrown an exception.");
   } catch (IllegalArgumentException e) {
     assertTrue("Actual msg: " + e.getMessage(), e.getMessage().contains("-1,-2,-3"));
   }
 }
 @Test
 public void whenNumberContainNegative() {
   String exMessage = "";
   try {
     cal.add("-3,6,4,-2");
   } catch (RuntimeException e) {
     exMessage = e.getMessage();
   }
   assertEquals("Negatives not allowed: [-3, -2]", exMessage);
 }
 @Test
 public final void whenNegativeNumbersAreUsedThenRuntimeExceptionIsThrown() {
   RuntimeException exception = null;
   try {
     StringCalculator.add("3,-6,15,-18,46,33");
   } catch (RuntimeException e) {
     exception = e;
   }
   Assert.assertNotNull(exception);
   Assert.assertEquals("Negatives not allowed: [-6, -18]", exception.getMessage());
 }
 @Test
 public final void whenDelimiterIsSpecifiedThenItIsUsedToSeparateNumbers() {
   Assert.assertEquals(3 + 6 + 15, StringCalculator.add("//;n3;6;15"));
 }
 @Test
 public final void whenAnyNumberOfNumbersIsUsedThenReturnValuesAreTheirSums() {
   Assert.assertEquals(3 + 6 + 15 + 18 + 46 + 33, StringCalculator.add("3,6,15,18,46,33"));
 }
 // Requirement 3: Method will return their sum of number
 @Test
 public final void whenOneNumberIsUsedThenReturnValueIsThatSameNumber() {
   Assert.assertEquals(3, StringCalculator.add("3"));
 }
 @Test(expected = RuntimeException.class)
 public final void whenNonNumberIsUsedThenExceptionIsThrown() {
   StringCalculator.add("1,X");
 }
 @BeforeClass
 public static void setUp() {
   cal = StringCalculator.getInstance();
 }
 @Test()
 public void whenIgnoreBiggerThan1000() {
   assertEquals(9, cal.add("//;1;3,1000,1001\n5"));
 }
 @Test
 public void whenNumberIsCorrect() {
   assertEquals(10, cal.add("5,5"));
 }
 @Test
 public final void when2NumbersAreUsedThenNoExceptionIsThrown() {
   StringCalculator.add("1,2");
   Assert.assertTrue(true);
 }
 @Test
 public void whenNumberIsEmpty() {
   assertEquals(0, cal.add(""));
 }
 // Requirement 2: For an empty string the method will return 0
 @Test
 public final void whenEmptyStringIsUsedThenReturnValueIs0() {
   Assert.assertEquals(0, StringCalculator.add(""));
 }
 @Test
 public void whenHaveOneNumber() {
   assertEquals(2, cal.add("2"));
 }
 @Test
 public final void whenTwoNumbersAreUsedThenReturnValueIsTheirSum() {
   Assert.assertEquals(3 + 6, StringCalculator.add("3,6"));
 }
 @Test()
 public void whenNumber2Numbers() {
   assertEquals(5, cal.add("2,3"));
 }
 @Test
 public final void whenNewLineIsUsedBetweenNumbersThenReturnValuesAreTheirSums() {
   Assert.assertEquals(3 + 6 + 15, StringCalculator.add("3,6n15"));
 }
 @Test()
 public void whenNumberOfNumberIsUnknow() {
   assertEquals(2 + 3 + 3 + 5 + 6 + 7 + 2, cal.add("2,3,3,5,6,7,2"));
 }
 @Test(expected = RuntimeException.class)
 public final void whenNegativeNumberIsUsedThenRuntimeExceptionIsThrown() {
   StringCalculator.add("3,-6,15,18,46,33");
 }
 @Test()
 public void whenNewLineInsteadOfCommas() {
   assertEquals(9, cal.add("1\n2,3\n3"));
 }
 @Test
 public final void whenOneOrMoreNumbersAreGreaterThan1000IsUsedThenItIsNotIncludedInSum() {
   Assert.assertEquals(3 + 1000 + 6, StringCalculator.add("3,1000,1001,6,1234"));
 }
 @Test()
 public void whenChangeOtherDelimiter() {
   assertEquals(7, cal.add("//;\n2;5"));
 }