Попытка написать модульный тест не может решить это? - PullRequest
0 голосов
/ 11 октября 2018

Написать юнит-тест для addInventory().Вызовите redSweater.addInventory() с параметром sweaterShipment.Распечатайте показанную ошибку, если последующее количество неверно.Пример вывода для неудачного модульного теста с заданным начальным количеством 10, а sweaterShipment - 50:

Начало испытаний.UNIT TEST FAILED: 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 ]

0 голосов
/ 11 октября 2018

Это похоже на домашнюю работу.

Я думаю, что ваш учитель ожидает простой тест по математике, чтобы показать, что 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 или менее предметов в инвентарь.

0 голосов
/ 25 октября 2018

Это решение, которое ищет класс (мне потребовалось время, чтобы выяснить, какое именно решение они хотели):

// ===== 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 =====
0 голосов
/ 11 октября 2018

Ваш код должен находиться в 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");
   }

 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...