Kenneth R. answered 06/21/24
Tutor
New to Wyzant
Math and Physics Tutoring
This is rather simple, since you are only looking to test for a match. Assuming the Product class
has an id property that is a String:
public class IdLookup implements Lookup
{
private String newId;
public IdLookup(String newId)
{
this.newId=newId;
}
public boolean matches (Product product)
if (product == null) return false;
else if (product.id == null){
return product.id == newId;
}else {
return product.id.equals(newId);
}
}
Kenneth R.
One more comment on testing, you also want to test versus the product itself being null as that is one branch of the code. So: assertNotTrue(lookup1.matches(null));
Report
06/21/24
Kenneth R.
As for testing, you will need to add a couple of Product objects to have a Products to compare against. You can set these as properties of the test class. On the test method itself, test various ID values against these products with some matching and some not. The test would be assertTrue(lookup1.matches(product1)) where lookup1 is an object already created with an id and product1 is one of the products set as a property of the test class. Also make sure you test against a null lookup id and a product with a null id. You might also want to do a test for when a product.id of null is matched with a lookup id of null.06/21/24