public int available(Item pItem)
{
if( aInventory.containsKey(pItem))
{
return aInventory.get(pItem);
}
else
{
return 0;
}
}
Java Junit
Write a test suite to test function 'available' in 'inventory' tp ensure 100% statement coverage in 'available' function to ensure 100% statement coverage in 'available' function. do not use the stock function, instead use reflection
Solution:
Please note since the implementation of Item object is not mentioned certain assumptions have been taken which are mentioned as comments.
Please find below the code:
TestClassForAvailableMethod.java
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestClassForAvailableMethod {
Map<Item, Integer> aInventory = null;
Item pItemObj1 = null;
Item pItemObj2 = null;
Item pItemObj3 = null;
/**
* this method will run before any other
* test method of this class
* executes
*/
@BeforeClass
public void setUp(){
aInventory = new
HashMap<>();
pItemObj1 = new Item(); // assuming
Item class have a no-arg constructor
pItemObj2 = new Item(); // assuming
Item class have a no-arg constructor
pItemObj3 = new Item(); // assuming
Item class have a no-arg constructor
aInventory.put(pItemObj1, 1); //
inserting first Item object in the aInventory map
aInventory.put(pItemObj2, 2);//
inserting second Item object in the aInventory map
}
/**
* this method tests if the
* inventory contains the given
* item and asserts if it returned
* correct 'value of the corresponding item
object
* in the aInventory' in this case 1
*/
@Test
public void testAvailableWithKeyPresent(){
Assert.assertEquals(1,
available(pItemObj1));
}
/**
* this method tests if the
* inventory contains the given
* item and asserts if it returned
* correct result i.e. 0
*/
@Test
public void testAvailableWithoutKeyPresent(){
Assert.assertEquals(0,
available(pItemObj3));
}
}
To achieve 100% statement coverage for the 'available' function using JUnit without using the stock function and using reflection, you can write multiple test cases to cover all possible code paths in the function. Here's an example of how to create a test suite for the 'available' function:
First, ensure you have the necessary imports:
javaCopy codeimport static org.junit.Assert.assertEquals;import java.lang.reflect.Method;
Now, create a test class for the 'available' function:
javaCopy codepublic class InventoryTest { private Inventory inventory; // Replace 'Inventory' with the actual class name @Before public void setUp() { // Create an instance of the 'Inventory' class to test inventory = new Inventory(); // Replace 'Inventory' with the actual class name } @Test public void testAvailableWithExistingItem() { // Test when the item exists in the inventory // Add the item to the inventory Item item = new Item("item1"); // Replace 'Item' with the actual item class inventory.aInventory.put(item, 5); // Assuming 'aInventory' is the map storing items and their quantities // Call the 'available' function using reflection int result = invokeAvailableFunction(item); // Check if the result matches the expected value assertEquals(5, result); } @Test public void testAvailableWithNonExistingItem() { // Test when the item does not exist in the inventory // Call the 'available' function using reflection with a non-existing item int result = invokeAvailableFunction(new Item("nonExistingItem")); // Replace 'Item' with the actual item class // Check if the result matches the expected value (0) assertEquals(0, result); } // Helper method to invoke the 'available' function using reflection private int invokeAvailableFunction(Item item) { try { // Get the 'available' method using reflection Method availableMethod = Inventory.class.getDeclaredMethod("available", Item.class); // Replace 'Inventory' with the actual class name // Set the method accessible (since it may be private) availableMethod.setAccessible(true); // Invoke the method on the 'inventory' object with the given item parameter return (int) availableMethod.invoke(inventory, item); } catch (Exception e) { // Handle any exceptions (e.g., reflection-related or method not found) e.printStackTrace(); } return -1; // Return a default value or handle error case appropriately } }
In the above test suite, we have two test cases: one for when the item exists in the inventory and another for when the item does not exist. The invokeAvailableFunction
method uses reflection to access and invoke the 'available' function on the 'inventory' object.
Ensure that you replace 'Inventory' with the actual class name and 'Item' with the actual item class name in the test suite according to your code implementation.
public int available(Item pItem) { if( aInventory.containsKey(pItem)) { return aInventory.get(pItem); } else { return 0; }...
Consider the following code: 1. float foo (int a, int b, int c, int d, float e) { 2. float e; 3. if (a == 0) { 4. return 0; 5. } 6. int x = 0; 7. if ((a==b) || ((c == d) && bug(a) )) { 8. x=1; 9. } 10. e = 1/x; 11. return e; 12. } Function bug(a) should return a value of true when passed a value of a=1. Build: • a test suite...
write java function: public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item if the item is stored in the tree. If the item is not in the tree, the function will return nu public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item...
Write Junit test for the following class below: public class Player { public int turnScore; public int roundScore; public int lastTurnScore; public String name; public int chipPile; public Player (String name) { this.name = name; this.turnScore = 0; this.chipPile = 50; } public int getChip() { return chipPile; } public void addChip(int chips) { chipPile += chips; } public int getRoundScore() { return roundScore; } public void setRoundScore(int points) { roundScore += points; } public int getTurnScore() { return turnScore; ...
Write Junit test for the following class below: public class Player { public int turnScore; public int roundScore; public int lastTurnScore; public String name; public int chipPile; public Player (String name) { this.name = name; this.turnScore = 0; this.chipPile = 50; } public int getChip() { return chipPile; } public void addChip(int chips) { chipPile...
Write Junit Test for the following class below: public class Turn { private int turnScore; private Dice dice; private boolean isSkunk; private boolean isDoubleSkunk; public Turn() { dice = new Dice(); } public Turn(Dice dice) { this.dice = dice; } public void turnRoll() { dice.roll(); if (dice.getDie1Value() == 1 || dice.getDie2Value() == 1 && dice.getLastRoll() != 2) { turnScore = 0; isSkunk = true; } else if (dice.getLastRoll() == 2) { turnScore = 0; isDoubleSkunk = true; } else { ...
b) Consider the following code. public static int f(int n) if (n == 1) return 0; else if (n % 2 == 0). return g(n/2); else return g(n+1); public static int g(int n) int r = n % 3; if (r == 0) return f(n/3); else if (r == 1) return f(n+2); else return f(2 * n); // (HERE) public static void main(String[] args) { int x = 3; System.out.println(f(x)); (1) (5 points) Draw the call stack as it would...
Int h(int n) { if (n == 0) return 0; else return h(n/10) + n%10;} What is the output for each function call (a) Function call h(736). Output: (b) Function call h(12345). Output: Answer options: 16,15 26,25 36,35 46,45
public class PQueue<E extends Comparable<E>> { private E[] elements; private int size; private int head; private int tail; Private int count; } public void enqueue(E item) { if(isFull()){ return; } count++; elements[tail] = item; tail = (tail + 1) % size; } public E dequeue() { if(isEmpty()) return null; int ct = count-1; E cur = elements[head]; int index = 0; for(i=1;ct-->0;i++) { if(cur.compareTo(elements[head+i)%size])<0) cur = elements[(head+i)%size]; index = i; } } return remove((head+index%size); public E remove(int index) { E...
Given following method, public static double getQuotient (int num1, int num2){ return num1/(double) num2; } Write Java statement to invoke the method.
public class F2{ public static int foo(ArrayList<Integer> a) { int sum = 0; for(int i = 0; i <a.size(); i++){ if(i % 3 == 1) sum += a.get(i); else if(i % 3 == 2) sum -= a.get(i); else sum++; } return sum; } } What do the following method calls return? 1. foo(new ArrayList<>(Arrays.asList(1,2,3,4))) 2. foo(new ArrayList<>()) 3. foo(new ArrayList<>(Arrays.asList(1,2,-3,4325,-2))) 4. foo(new ArrayList<>(Arrays.asList(0,0,0)))