Question

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
Add a comment Improve this question Transcribed image text
Answer #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 =====

> // FIXME add unit test for addInventory

Charles Adams Mon, Feb 28, 2022 4:18 PM

Add a comment
Answer #2

well you cant change the code about // FIXME add unit test for addInventory

so thanks for practically nothing


Add a comment
Know the answer?
Add Answer to:
JAVA Directions: Write a unit test for addInventory(), which has an error. Call redSweater.addInventory() with parameter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in....

    Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in. The main method prompts for a salary, then uses a TaxTableTools method to get the tax rate. The program then calculates the tax to pay and displays the results to the user. Run the program with annual salaries of 10000, 50000, 50001, 100001 and -1 (to end the program) and note the output tax rate and tax to pay. a. Modify the TaxTableTools class...

  • I need help with a java error Question: Consider a graphics system that has classes for...

    I need help with a java error Question: Consider a graphics system that has classes for various figures—say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and center point, while a box and circle might have only a center point and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure. You are to implement such a system. The class Figure is...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Identify a logical error in the following code and fix it. public class test1 {    ...

    Identify a logical error in the following code and fix it. public class test1 {     public static void main(String[] args){         int total;         float avg;         int a, b, c;         a=10;         b=5;         c=2;         total=a+b+c;         avg=total/3;         System.out.println("Average: "+avg);     } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

  • Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called...

    Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT