Cobertura refuses to acknowledge code was covered

I am using the Maven (2) Cobertura plug-in to create reports on code coverage, and I have the following stub I am using in a method:

try {
} catch (final SecurityException exception) {
System.err.println("The program never exited!");

I know that I need to log the exception, etc, but that's not the point right now...Cobertura is refusing to acknowledge that the line after the stack trace is printed is covered. That is, the line with the '}' before the System.err.println statement is not being shown as covered. Before, the ending curly brace of the method was not being shown as covered, hence the System.err statement. Any idea how I can convince cobertura's maven plugin that, since the System.err.println statement is covered, that ending brace has to have been covered?

Oh yeah, and I use a mock security manager to throw the security exception, since that's the easiest way I have found of making the test continue executing after the System.exit call.

Asked by: Emma734 | Posted: 23-01-2022

Answer 1

I would look at the coverage report. Double check my tests. Notice that the code really is getting covered and not worry about hitting 100%. Code coverage is best used to find areas that you may have neglected to hit with your tests, but just focusing on getting 100% coverage as a goal is bad habit that can lead to you skipping tests that need to be written just because your tool shows 100%. Use the tool for what it can do but don't fall into the trap of letting the tool define what you do.

Answered by: Dainton521 | Posted: 24-02-2022

Answer 2

In the Java classfile format every method is annotated with a table mapping code offsets to line numbers. In this case, the closing brace does not produce any bytecode, hence it's not covered. This is an issue of imperfect correspondence between source and bytecode. It should be handled by the coverage tool, recognizing this line as non-code.

I know that Emma has similar issues. Clover fares much better, but is commercial (not sure if it would handle this case also). If you use IDEA, you should try their new coverage implementation - it's quite good and in active development.

Answered by: Ryan499 | Posted: 24-02-2022

Answer 3

I haven't used Cobertura in a while (2005?), and saw this behavior back then. A similar problem exists with NCover for C# and curly braces following catch/finally blocks.

My suggestion would be to add to this Cobertura bug report detailing a similar issue. Also, follow @tvanfosson's advice and realize not having coverage on a curly brace, which doesn't actually become anything in the JVM, is something you can ignore as 'noise'.

Answered by: Adrian460 | Posted: 24-02-2022

Answer 4

I know this is an old question and that Cobertura has already fixed this, but for completeness the missing coverage on the "}" was caused by the internal automatically "finally" block.

See your code as this:

try {
} catch (final SecurityException exception) {
} finally {
    // noop

Fortunately this is no longer happening for some versions.

Answered by: Jack802 | Posted: 24-02-2022

Similar questions

java - Acknowledge from JAX-RS client

I expose some JAX-RS (Resteasy) web services to a remote client. One in particular is a service like "give me all news since the last time I asked you"; on the server I do my queries and all, and if everything went fine I update a property on the client which tells me future requests will start from that date. The problem here is that if something goes wrong while returning the answer I could already have u...

java - Why doesn't an executable jar acknowledge jars inside it?

I made an executable jar that depends on other jars with the command prompt using the format jar cvfm MyJarName.jar manifest.txt *.class dependentJar1.jar dependentJar2.jar The jar was made properly and everything seemed fine... But when run, it gets runtime errors because it can't find the class files that my project refers to. Exploring the created jar, the other 2 jars that it depends o...

paypal:how to acknowledge my application for every paypal recurring payment period in soap java

how my java application ackowledges every billing period transaction simply for every month how my application knows whether transaction is succeeded or not .ex: for every 2nd of month i bill must pay in that day how my application executes without manual checkings

java - How to acknowledge JMS messages with JTA on commit?

I struggeling with JTA, two-phase-commit, JMS- and JDBC-transactions. The idea is (in short) to receive a message on a queue perform some database operations acknowledge the message, when the db operations have been successful So I got the XAQueueConnectionFactory, create the XAQueueSession, create a receiver from the session and set a message listen...

java - Acknowledge receipt of producer message by client

I have a Java class implementing a message producer, which sends messages to a queue named test. I have another class serving as a client, which gets messages from the same queue. I am confused on how to know if the message has been received at the client side. I read somewhere that I should use message.acknowledge(), but I do not know the proper way to do this. My code is shown below: Producer:

java - How to acknowledge messages in Message Driven Beans

In the JMS documentation I read that Message Driven Beans doesn't support CLIENT_ACKNOWLEDGE mode, only DUPS_OK_ACKNOWLEDGE and AUTO_ACKNOWLEDGE. As I understand it, in AUTO_ACKNOWLEDGE mode, the message is acknowledged (deleted from the destination) when the onMessage method is invoked. What I want is to tell my broker not to delete messages from the destination ...

java - How to not acknowledge only one message with Spring-JMS?

There is a class 'MyConsumer' which receives messages from a queue, and processes them. There are two requirements: If there is a message contains invalid content, MyConsumer should not acknowledge it, but can process later messages The unconsumed message will be deliver again when MyConsumer restarts I tried with spring-jms, with the listener-container supports, but can't find a solut...

java - Set up a topic without auto acknowlege and hand acknowledge upon receipt

The JMS docs for Spring 4 here: don't seem to cover this. I have multiple clients connecting with sockets to a socket server. They send messages to my server and receive messages back, al...

java - ActiveMQ throwing Unmatched acknowledge exception

WARN | Async error occurred: javax.jms.JMSException: Unmatched acknowledge: MessageAck {commandId = 30, responseRequired = false, ackType = 2, consumerId = ID:PC02-DT-009-52405-1421153309991-1:3:10:1, firstMessageId = ID:PC02-DT-009-52360-1421152976467-1:4:11:1:1, lastMessageId = ID:PC02-DT-009-52360-1421152976467-1:4:11:1:1, destination = queue://DATA.DESTINATION, transactionId = null, messageCount = 1, poisonCaus...

java - JMS - How to correctly implement message receiver with explicit acknowledge?

The JMS API is driving me to the point of a mental breakdown.. What is the correct way to poll messages of a jms queue and explicitly acknowledge that each message has been processed correctly? Should I create a "transacted session" and which acknowledge mode is the correct one in the scenario described above? My design constraints: Duplicate delivery is no problem Performance is no...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)