英文:
How to get full coverage for Java Code Coverage? Junit test cases
问题
我正在为一门课程做作业,我需要完全覆盖这个方法
![在Eclipse中使用JaCoco进行代码覆盖率的截图][1]
这些是属性和构造函数,这是咖啡机的程序,这是Recipe类
public class Recipe {
private String name;
private int price;
private int amtCoffee;
private int amtMilk;
private int amtSugar;
private int amtChocolate;
/**
* 为咖啡机创建一个默认的配方。
*/
public Recipe() {
this.name = "";
this.price = 0;
this.amtCoffee = 0;
this.amtMilk = 0;
this.amtSugar = 0;
this.amtChocolate = 0;
}
我使用了
/*
* setPrice 测试
*/
@Test
public void testSetPrice_1() throws RecipeException {
r1.setPrice("25");
r1.setPrice("0");
}
/*
* setPrice 测试
*/
@Test(expected = RecipeException.class)
public void testSetPrice_2() throws RecipeException {
r1.setPrice("adsada");
r1.setPrice(" ");
r1.setPrice("-1");
}
当我使用 RecipeException 时,似乎无法捕获 recipeException,即使我知道它会被抛出,覆盖范围也无法达到整个方法。
这个类是唯一一个没有完全覆盖率的类,而这个 RecipeException 似乎没有被捕获。
当 RecipeException 被抛出时,我应该如何进行测试,以实现完全覆盖?
这段代码属于课程 edu.ncsu.csc326.coffeemaker
英文:
I am doing an assignment for a course, I need to get full coverage of this method
These are the attributes and the constructor, it is a program for a coffee machine, this is the Recipe class
public class Recipe {
private String name;
private int price;
private int amtCoffee;
private int amtMilk;
private int amtSugar;
private int amtChocolate;
/**
* Creates a default recipe for the coffee maker.
*/
public Recipe() {
this.name = "";
this.price = 0;
this.amtCoffee = 0;
this.amtMilk = 0;
this.amtSugar = 0;
this.amtChocolate = 0;
}
I've used
/*
* setPrice test
*/
@Test
public void testSetPrice_1() throws RecipeException {
r1.setPrice("25");
r1.setPrice("0");
}
/*
* setPrice test
*/
@Test(expected = RecipeException.class)
public void testSetPrice_2() throws RecipeException {
r1.setPrice("adsada");
r1.setPrice(" ");
r1.setPrice("-1");
}
The recipeException doesn't seem to be catching when I use RecipeException and even thought I know it's going to be thrown the coverage doesn't get to the entire method.
This class is the only one left with not full coverage and this RecipeException doesn't seem to be cathing.
How should I do the test when the RecipeException is thrown so it gets full coverage?
This code belongs to the course edu.ncsu.csc326.coffeemaker
答案1
得分: 5
您的测试失败是因为在testSetPrice_2
方法中,对r1.setPrice("adsada");
的初始调用引发了NumberFormatException
,从而中断了测试的执行...
r1.setPrice(" ");
r1.setPrice("-1");
因此这两行代码永远不会执行。要解决这个问题,您需要将每次对r1.setPrice(...)
的调用放入一个单独的测试方法中,例如如下所示:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
public class RecipeTest {
Recipe r1;
@Before
public void setUp() throws Exception {
r1 = new Recipe();
}
@Test
public void testSetPriceValid_1() throws RecipeException {
r1.setPrice("25");
}
@Test
public void testSetPriceValid_2() throws RecipeException {
r1.setPrice("0");
}
@Test(expected = RecipeException.class)
public void testSetPriceInvalid0() throws RecipeException {
r1.setPrice("adsada");
}
@Test(expected = RecipeException.class)
public void testSetPriceInvalid1() throws RecipeException {
r1.setPrice(" ");
}
@Test(expected = RecipeException.class)
public void testSetPriceInvalid2() throws RecipeException {
r1.setPrice("-1");
}
}
英文:
Your test is failing because in the testSetPrice_2 method, the initial invocation of r1.setPrice("adsada");
causes a NumberFormatException
to be thrown which interrupts the execution of the test ...
r1.setPrice(" ");
r1.setPrice("-1");
are thus never run. To resolve this you need to make each invocation of r1.setPrice(...)
a separate test method, e.g. as shown below:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
public class RecipeTest {
Recipe r1;
@Before
public void setUp() throws Exception {
r1 = new Recipe();
}
@Test
public void testSetPriceValid_1() throws RecipeException {
r1.setPrice("25");
}
@Test
public void testSetPriceValid_2() throws RecipeException {
r1.setPrice("0");
}
@Test(expected = RecipeException.class)
public void testSetPriceInvalid0() throws RecipeException {
r1.setPrice("adsada");
}
@Test(expected = RecipeException.class)
public void testSetPriceInvalid1() throws RecipeException {
r1.setPrice(" ");
}
@Test(expected = RecipeException.class)
public void testSetPriceInvalid2() throws RecipeException {
r1.setPrice("-1");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论