- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一门类(class)做作业,我需要全面了解此方法
这些是属性和构造函数,这是一个咖啡机程序,这是 Recipe 类
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;
}
我用过
/*
* 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");
}
当我使用 RecipeException 时,recipeException 似乎没有被捕获,甚至认为我知道它会被抛出,但覆盖范围没有到达整个方法。
这个类是唯一一个没有完全覆盖的类,而且这个 RecipeException 似乎没有任何内容。
当 RecipeException 抛出时,我应该如何进行测试以使其得到全面覆盖?
此代码属于edu.ncsu.csc326.coffeemaker类(class)
最佳答案
您的测试失败是因为在 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");
}
}
关于java - 如何获得 Java 代码覆盖率的全面覆盖? Junit 测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63577572/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!