below is code and I want to know how can I write a JUnit test so that my coverage can cover catch block
import com.pkg.ClothBrandQuery;
Public class MapBrand implements SomeInterface{
public Brand mapThisBrands(final ClothBrand clothBrand) {
Brand brand = new Brand();
try{
defaultBrand = clothBrandQuery.getClothBrandMethod(ProjectConstants.DEFAULT_BRAND_KEY);
}
catch(Exception e){
logger.fatal("Database error occurred retrieving default cloth brands", e);
System.exit(2);
}
if(defaultBrand != null && defaultBrand != clothBrand){
brand.setBrandName(clothBrand.getBrandName());
brand.setCompanyName(clothBrand.getCompanyName());
brand.setBackgroundColor(clothBrand.getBackgroundColor());
}
else{
brand.setBrandName(defaultBrand.getBrandName());
brand.setCompanyName(defaultBrand.getCompanyName());
brand.setBackgroundColor(defaultBrand.getBackgroundColor());
}
}
}
I am able to write test method for mapThisBrands to test brand object but I dont know how I can test defaultBrand object and how I write a testcase where catch block will get executed.
System.exit()anywhere except the top-levelmain()method with the only purpose to return an exit code. Always use exceptions in all other cases. See javapractices.com/topic/TopicAction.do?Id=86.