为addInventory()
编写单元测试。使用参数redSweater.addInventory()
调用sweaterShipment
。如果后续数量不正确,请打印显示的错误。给定初始数量为10且sweaterShipment
为50时,失败的单元测试的样本输出:
开始测试。单元测试失败:addInventory()测试完成。注意: 单元测试失败前有3个空格。
// ===== Code from file InventoryTag.java =====
public class InventoryTag {
private int quantityRemaining;
public InventoryTag() {
quantityRemaining = 0;
}
public int getQuantityRemaining() {
return quantityRemaining;
}
public void addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}
}// ===== end =====
// ===== Code from file CallInventoryTag.java =====
public class CallInventoryTag {
public static void main (String [] args) {
InventoryTag redSweater = new InventoryTag();
int sweaterShipment;
int sweaterInventoryBefore;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
System.out.println("Beginning tests.");
// FIXME add unit test for addInventory
System.out.println("Tests complete.");
}
}// ===== end =====
答案 0 :(得分:0)
对于源代码,您的代码应位于src/main
中,对于测试,则应位于src/test
中。
然后,当您为A
中位于package a;
上的src/main
类添加测试时,您就在ATest
中的package a;
中编写了src/test
。
在您的示例中,测试类应类似于:
public class CallInventoryTagTest {
@Test(expected=YourException.class)
public static void shouldThrowYourExceptionWhenX () {
//given
InventoryTag redSweater = new InventoryTag();
int sweaterShipmen=25;
int sweaterInventoryBefore;
//when
// that's what you need to write after your FIXME
sweaterInventoryBefore = redSweater.getQuantityRemaining();
redSweater.addInventory(sweaterShipmen) //calling addinventor with parameter sweaterShipment
//then
fail("should throw an error because of X");
}
}
答案 1 :(得分:0)
感觉就像是作业。
我认为您的老师希望通过简单的数学测试来表明InventoryTag未初始化 如果提供的数字小于或等于10,则成功。
类似的东西:
// ===== Code from file CallInventoryTag.java =====
public class CallInventoryTag {
public static void main (String [] args) {
InventoryTag redSweater = new InventoryTag();
int sweaterShipment;
int sweaterInventoryBefore;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterInventoryBefore = 10;
sweaterShipment = 50;
System.out.println("Beginning tests.");
// FIXME add unit test for addInventory
redSweater.addInventory(sweaterInventoryBefore);
redSweater.addInventory(sweaterShipment);
if (sweaterInventoryBefore + sweaterShipment != redSweater.getQuantityRemaining()) {
System.out.println(" UNIT TEST FAILED: addInventory()");
}
System.out.println("Tests complete.");
}
}// ===== end =====
测试失败,因为方法addInventory中的条件阻止向清单中添加10个或更少的项目。
答案 2 :(得分:0)
这是班级正在寻找的解决方案(花了我一段时间才弄清楚他们想要的特定解决方案):
// ===== Code from file InventoryTag.java =====
public class InventoryTag {
private int quantityRemaining;
public InventoryTag() {
quantityRemaining = 0;
}
public int getQuantityRemaining() {
return quantityRemaining;
}
public void addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}
}
// ===== end =====
// ===== Code from file CallInventoryTag.java =====
public class CallInventoryTag {
public static void main (String [] args) {
InventoryTag redSweater = new InventoryTag();
int sweaterShipment;
int sweaterInventoryBefore;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
System.out.println("Beginning tests.");
// FIXME add unit test for addInventory
/* Your solution starts here */
redSweater.addInventory(sweaterShipment);
if (sweaterInventoryBefore + sweaterShipment != redSweater.getQuantityRemaining()) {
System.out.println(" UNIT TEST FAILED: addInventory()");
}
/* End of your solution*/
System.out.println("Tests complete.");
}
}
// ===== end =====