Search code examples
javamaven-cobertura-plugin

Cobertura Java7 try with resource


i'm using cobertura 2.6 with maven on java 1.7

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                </configuration>
            </plugin>

but if i use the new try-with-resource feature of java7 it tells me the "non existing catch" block is missing in tests... it marks the closing bracket of the try-block

any ideas what's wrong? or how i can test them?


Solution

  • The problem is that you are probably not testing all the cases for your try with resources block. Any time that you write something like:

    try(Autocloseable ac = new Autocloseable()) {
       //do something
    } catch(Exception e) {
       //Do something with e
    }
    

    The compiler interprets something like:

    Autocloseable ac = null;
    Exception e = null;
    try {
       ac = new Autocloseable();
       //Do something
    } catch (Exception e1) {
       e = e1
       //Do something with exception
    } finally {
      if(ac != null) {
         try {
           ac.close();
         } catch (Exception e2) {
            throw e == null? e2 : e;
         }
         if(e != null ) throw e; 
      }
    }
    

    It is not exactly like that, but is the overall idea, so you see how the actual code branches are a lot more than what you thought could be. I hope this gives you an idea on how to improve your coverage.