1

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.

2

2 Answers 2

2

Put that System.exit(2) code in its own class and stub or mock it out instead of calling that code directly. This way, in your test it won't really exit, but you'll know that in a production environment it will.

If you don't know how to go about this (mock or stub), you should learn more about dependency injection.

Now you're half done. The other half is to use dependency injection to mock out clothBrandQuery. Make it so that its getClothBrandMethod throws an exception no matter what. Then you will go down this path. A good mocking framework to use is Mockito.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I did that. Thanks for your help.
1

You shouldn't use System.exit() in that method, only use it in a main method and then throw an exception in mapThisBrands:

public class MapBrand implements SomeInterface{

    public static void main(String[] args) {
        MapBrand map = new MapBrand();
        ClothBrand clothBrand = this.getClothBrand();
        try
        {
            map.mapThisBrands(clothBrand);
        }
        catch (Exception e)
        {
            System.exit(2);
        }
    }

    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);
        }
        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());
        }
    }
}

And your test

public class MapBrandTestCase {

    @Test (expected = java.lang.Exception.class)
    public void databaseFailureShouldRaiseException()
    {
        Query clothBrandQuery = Mockito.mock(Query.class);
        when(clothBrandQuery.getClothBrandMethod(any())).thenThrow(new Exception());
        MapBrand mapBrand = new MapBrand(...);
        mapBrand.mapThisBrands(aBrand);
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.