如何获得Java代码覆盖率的完整覆盖?Junit测试用例

huangapple go评论124阅读模式
英文:

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

如何获得Java代码覆盖率的完整覆盖?Junit测试用例

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");
}
}

huangapple
  • 本文由 发表于 2020年8月25日 19:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63577572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定