-1

hey guys i have this Junit test code for factorial

@org.junit.Test
public void testIterationAAA()
{
    Iteration test = new Iteration("AAA");
    int result = test.factorial("AAA");

    assertEquals("exceptionMessage",result);

}

supposedly since a string's factorial cant be calculated the exception i made should be thrown but how to test it using Junit??

Mabulhuda
  • 57
  • 1
  • 6
  • possible duplicate of [How do you assert that a certain exception is thrown in JUnit 4 tests?](http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) – Ozan Dec 18 '14 at 19:52

2 Answers2

0
import org.junit.Assert;

...

@org.junit.Test
public void testIterationAAA()
{
    try {
         Iteration test = new Iteration("AAA");
         int result = test.factorial("AAA");
         // The above line is expected to throw an exception.
         // If the code does not throw an exception, fail the test.
         Assert.fail("An exception should have been thrown");
    } catch (Exception ex) {
         // Nothing to do.  Exception was expected in this test case.
    }

}
EJK
  • 12,332
  • 3
  • 38
  • 55
0

You should use expected attribute

@Test(expected=SomeException.class)
Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63