【问题标题】:Trying to write unit test cannot work it out?尝试编写单元测试无法解决?
【发布时间】:2019-03-16 02:17:33
【问题描述】:

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

开始测试。单元测试失败:addInventory() 测试完成。注意: UNIT TEST FAILED 前面有 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 =====

【问题讨论】:

  • 抱歉,您在哪里设置初始数量?什么是前面有3个空格,是什么意思?
  • 读后读 System.out.println("Beginning tests.");这就是我需要编写代码的地方。
  • 你在 System.out.println("Beginning tests."); 之后设置它;
  • 您在此处发布的内容不是正确的 Java 单元测试。您可以通过运行 main 方法并运行一些东西来练习 Java 代码,但下面@vmrvictor 的回答显示了正确的方法。您的 Test 注释失败的原因是因为您需要导入正确的库/类,并且任何体面的 IDE 都会为您做到这一点。
  • @Oceanman 不要以这种方式破坏您的问题。这是不可接受的行为!请注意,除非给出了赞成或接受的答案,否则您可以简单地删除您的问题。

标签: java unit-testing


【解决方案1】:

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

// ===== 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 =====

【讨论】:

    【解决方案2】:

    感觉就像做作业。

    我认为您的老师希望通过简单的数学测试来显示未启动 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】:

    您的代码应位于 src/main 以获取源代码,src/test 用于测试。 然后,当您在 package a; 并位于 src/main 中添加类 A 的测试时,您在位于 package a;package a; 中写入 ATest

    在您的示例中,测试类应类似于:

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

    【讨论】:

    • 我需要在 System.out.println("Beginning tests."); 之后写代码我想不出任何办法来解决它。我真的需要这个,很紧急,请回复。
    • @Oceanman 你已经写过,在你的测试中你必须使用参数毛衣发货调用 addInventory --> 到这里就完成了,但我不明白你为什么需要一个错误
    • 一个错误对我来说是一个异常,如果你只是抛出一个异常,那么你的测试将失败,你必须要么抓住它,要么使用这个说明你期望这个错误的注释。
    • 开始测试。单元测试失败:addInventory() 测试完成。
    • 如果你想得到这个输出你只需要写 --> System.out.println("Beginning tests. UNIT TEST FAILED: addInventory() Tests complete.") //但是为什么你需要这个输出,为什么会失败?
    猜你喜欢
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    相关资源
    最近更新 更多