d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Junit Expected Exception Handling > One Is Working, The Other Isn't
Add Reply New Topic New Poll
Member
Posts: 21
Joined: Mar 28 2013
Gold: 0.00
May 11 2014 12:32pm
I'm getting red bar with testDivide2, but everything's cool with divisionWithException. Does anyone know why ?
TestUnit:
Code

public class TestCaseArithmetic extends TestCase {

@Test(expected = ArithmeticException.class)
public void testDivide2(){
int i =Arithmetic.divide(1, 0);
}
@Test(expected = ArithmeticException.class)
public void divisionWithException() {
int i = 1/0;
}
@Test(timeout=1)
public void testSum(){
assertEquals(2,Arithmetic.sum(1, 1));
}
@Test(timeout=1)
public void testDivide(){
assertEquals(2,Arithmetic.divide(5, 2));
}

}

Arithmetic:
Code
public class Arithmetic {
public static int sum(int a, int b){
return a+b;
}
public static int divide(int a,int b) {
return a/b;
}
}
Member
Posts: 16,411
Joined: Apr 9 2007
Gold: 12,059.17
May 14 2014 12:37pm
thats an unusual scenario, maybe your code uses a different Arithmetic class, one that doesnt throw this Exception. Maybe its in different package or project wasnt rebuilt.

Fix suggestion: debug this code and you will see whats wrong, otherwise its just guessing.

This post was edited by Ironfister on May 14 2014 12:43pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 14 2014 01:30pm
You need to declare the Arithmetic.divide method to throw the exception.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll