Homework Help Question & Answers

JAVA Directions: Write a unit test for addInventory(), which has an error. Call redSweater.addInventory() with parameter...

JAVA

Directions:

Write a unit test for addInventory(), which has an error. Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:

Beginning tests.
   UNIT TEST FAILED: addInventory()
Tests complete.

Note: UNIT TEST FAILED is preceded by 3 spaces.

What I have so far:

// ===== 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 =====
import java.util.Scanner;

public class CallInventoryTag {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
InventoryTag redSweater = new InventoryTag();
int sweaterShipment;
int sweaterInventoryBefore;

sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = scnr.nextInt();

System.out.println("Beginning tests.");

// FIXME add unit test for addInventory

if (redSweater.getQuantityRemaining() != sweaterShipment+sweaterInventoryBefore )
{
System.out.println(" UNIT TEST FAILED: addInventory()");
}

System.out.println("Tests complete.");
}
}
// ===== end =====

Error:

Inventory is 0, shipment input is 25. Testing that quantityRemaining was updated to 25.

Value differs. See highlights below.

Your value

0

Expected value

25

Testing with sweaterShipment input of 25. addInventory updates quantityRemaining.

Output differs. See highlights below. Special character legend

Your output

Beginning tests. UNIT TEST FAILED: addInventory() Tests complete.

Expected output

Beginning tests. Tests complete.

Inventory is 25, shipment input is 5. Testing that quantityRemaining remains 25.

Value differs. See highlights below.

Your value

0

Expected value

25

0 0
Next > < Previous
ReportAnswer #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
           System.out.println("Tests complete.");
       }

     }// ===== end =====

Your code should be located in src/main for source code and src/test for tests. Then when you add a test for a class A in package a; and located at src/main then you write ATest in package a; located in src/test.

in your example the test class should look similar to:

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

 }

that InventoryTag is not inited successfully when a number less than or equal to 10 is provided.

Something like:

// ===== 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 =====
Add Homework Help Answer
Add Answer of:
JAVA Directions: Write a unit test for addInventory(), which has an error. Call redSweater.addInventory() with parameter...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES
Related Questions