Java 尝试编写单元测试无法解决问题?

Java 尝试编写单元测试无法解决问题?,java,unit-testing,Java,Unit Testing,为addInventory()编写单元测试。使用参数sweaterShipment调用redSweater.addInventory()。如果后续数量不正确,则打印显示的错误。给定初始数量的失败单元测试的样本输出为10,sweaterShipment为50: 开始测试。单元测试失败:AddInEntory()测试已完成。注意: 单元测试失败前有3个空格 源代码应位于src/main,测试应位于src/test。 然后,当您为包a中的类a添加测试时并位于src/main处,然后在包a中写入ATes

addInventory()
编写单元测试。使用参数
sweaterShipment
调用
redSweater.addInventory()
。如果后续数量不正确,则打印显示的错误。给定初始数量的失败单元测试的样本输出为10,
sweaterShipment
为50:

开始测试。单元测试失败:AddInEntory()测试已完成。注意: 单元测试失败前有3个空格


源代码应位于
src/main
,测试应位于
src/test
。 然后,当您为
包a中的类
a
添加测试时并位于
src/main
处,然后在
包a中写入
ATest
位于
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");
   }

 }

感觉像是家庭作业

我想你的老师希望通过一个简单的数学测试来证明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个或更少的项目

这就是这个班正在寻找的解决方案(我花了一段时间才找到他们想要的特定解决方案):


对不起,您的初始数量是在哪里设置的?什么前面有3个空格,这意味着什么?在它说System.out.println(“开始测试”)之后阅读;这就是我需要编写代码使其工作的地方;您在这里发布的不是一个正确的Java单元测试。您可以通过运行main方法和运行things来练习Java代码,但是@vmrvictor下面的答案显示了正确的方法。测试注释失败的原因是您需要导入适当的库/类,任何合适的IDE都可以为您做到这一点。@Oceanman不要以这种方式破坏您的问题。这是不可接受的行为!请注意,您可以简单地删除您的问题,除非给出了经过投票或接受的答案。我需要在System.out.println(“开始测试”)之后编写代码;我想不出任何办法来解决它。我真的需要这个,很紧急,请回复。@Oceanman您在测试中写道,您必须调用addInventory,参数为sweaterShipment-->此处已完成,但我不明白您为什么需要错误我的错误是一个例外,如果您只是抛出一个异常,那么您的测试将失败。您必须捕获它,或者使用表示您预期会出现此错误的注释。开始测试。单元测试失败:AddInEntory()测试已完成。如果要获得此输出,只需编写-->System.out.println(“开始测试。单元测试失败:AddInEntory()测试已完成”。)//但为什么需要此输出,为什么失败?”感觉像是家庭作业。那又如何?这与答案无关
// ===== 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 =====
// ===== 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 =====