Question: package junit _ testing _ Jtorres 1 2 6 ; public class Product { / / class fields / / we will do data hiding.

package junit_testing_Jtorres126;
public class Product {
//class fields
//we will do data hiding. so all will start with private keywords
private String prodName;
private double prodPrice;
private int prodQty;
public Product(String prodName, double prodPrice){//constructor
this.prodName = prodName;
this.prodPrice = prodPrice;
}
public Product(){//no args constructor
}
public static void main(String[] args){
Product p1= new Product();
p1.setProdPrice(-20);
System.out.println(p1.getProdPrice());
}
public String getProdName(){
return prodName;
}
public void setProdName(String prodName){
this.prodName = prodName;
}
public double getProdPrice(){
return prodPrice;
}
public void setProdPrice(double prodPrice){
if(prodPrice <0){
this.prodPrice =0.25;
}
else {
this.prodPrice = prodPrice;
}
}
public int getProdQty(){
return prodQty;
}
public void setProdQty(int prodQty){
this.prodQty = prodQty;
}
public String toString(){
return this.getProdName()+","+this.getProdPrice();
}
}package junit_testing_Jtorres126;
import static org.junit.Assert.*;
import org.junit.Test;
public class ProductTest {
@Test
public void testPriceVar(){
Product prod = new Product ("Dr. Pepper", 1.25);
double expected =1.25;
double actual = prod.getProdPrice();
assertEquals(expected, actual, 0);
}
@Test
public void testPriceVarForLessThanZero(){
Product prod = new Product ("Dr. Pepper", -1.99);
double expected =0.25;
double actual = prod.getProdPrice();
assertEquals(expected, actual, 0);
}
}The next code is the Junit tester code that is recieving the fail for less than zero testpackage junit_testing_Jtorres126;
import static org.junit.Assert.*;
import org.junit.Test;
public class ProductTest {
@Test
public void testPriceVar(){
Product prod = new Product ("Dr. Pepper", 1.25);
double expected =1.25;
double actual = prod.getProdPrice();
assertEquals(expected, actual, 0);
}
@Test
public void testPriceVarForLessThanZero(){
Product prod = new Product ("Dr. Pepper", -1.99);
double expected =0.25;
double actual = prod.getProdPrice();
assertEquals(expected, actual, 0);
}
}Last are the actual instructions from the assignment e. Make sure there are no syntax errors then press the green Run button at the top of Eclipse. We see that one of our test passed and one failed. And in the Failure trace, we see the expected and the actual results.
f. Fix the setProdPrice(double) method of the Product class so it handles values less than zero as described by our rules. Think about if you are not setting the value to check, so You need need to figure out where to add the change.(Tips: Constructor)
g. Make sure there are no syntax errors then press the green Run button at the top of Eclipse. Did both test pass this time?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!