eclipseのプラグイン「EclEMMA」を使用してカバレッジを計ったのですが、テストクラスの例外をだす部分が実行されない扱いになるせいで、カバレッジが100%になりません。100%になる方法を教えてください。下の41~42行目が実行されない扱いになります。
Java
1package junit.tutorial; 2 3public class Calculator { 4 public int multiply(int x, int y) { 5 return x * y; 6 } 7 public float divide(int x, int y) { 8 if(y == 0) throw new IllegalArgumentException("divide by zero."); 9 return (float)x / (float)y; 10 } 11 public int add(int x, int y) { 12 return x + y; 13 } 14} 15
Java
1package junit.tutorial; 2 3import static org.hamcrest.CoreMatchers.*; 4import static org.junit.Assert.*; 5 6import org.junit.Test; 7public class CalculatorTest { 8 9 @Test 10 public void multiplyで乗算結果が取得できる() { 11 Calculator calc = new Calculator(); 12 int expected = 12; 13 int actual = calc.multiply(3, 4); 14 assertThat(actual, is(expected)); 15 } 16 @Test 17 public void multiplyで5と7の乗算結果が取得できる() { 18 Calculator calc = new Calculator(); 19 int expected = 35; 20 int actual = calc.multiply(5, 7); 21 assertThat(actual, is(expected)); 22 } 23 @Test 24 public void divideで3と2の除算結果が取得できる() throws Exception{ 25 Calculator calc = new Calculator(); 26 float expected = 1.5f; 27 float actual = calc.divide(3, 2); 28 assertThat(actual, is(expected)); 29 } 30 @Test 31 public void addのカバレッジ() { 32 Calculator calc = new Calculator(); 33 int expected = 5; 34 int actual = calc.add(2, 3); 35 assertThat(actual, is(expected)); 36 } 37 @Test(expected = IllegalArgumentException.class) 38 public void カバレッジ() { 39 Calculator calc = new Calculator(); 40 // 例外をだす 41 calc.divide(0, 0); 42 } 43}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/10 09:21