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...
---> JAVA PROGRAM Public Static int EXX (int n,int x){ If (x==0){ return 1; } else If (n==1) { return 1; } else { return (n* EXX(n,x-1)); * what is the output when (5,0) *what is the output when (8,1) what is the output when (4,3)
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...
1. public int function(int x, int n) { if (n == 0) return 1; return x * function(x, n -1); } function(3,3) - What is the expected output? 3 12 9 27 2. int fun(int x) { if(x == 0) return 1; else return fun(x - 1); } fun(4) 18 1 24 4 3. Which one of the following calls results 6? int mystery(int n){ if (n == 1) return 1; else return n * mystery(n - 1); } mystery(3)...
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 { ...
You only need to use Junit to test 3 methods of the Nurse class. public Nurse(int a, float s) { ... } public boolean isBusy() {...} public void retire() {....} code: public class Nurse { private int numOfPatients; private float salary; // Helping function // private void trace(char* s) { cout << s << endl; } // Implementor function public Nurse(int a, float s) { numOfPatients =a; salary=s; } // Access function public int getNumOfPatients() { return numOfPatients; } public...
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
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...