Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Clover / JMockit: invalid try/finally in expectations block

Hello,

 

I am trying to use Clover in my project but I have some problems with the maven-clover2-plugin.

While normal unit tests work fine I get an error if I try to instrument test classes which use the JMockit Expectations.

 

For example:

import java.util.regex.Matcher;
import mockit.Expectations;
import mockit.Mocked;
import org.junit.Test;

public class MyTest {

    @Mocked Matcher matcher;

    @Test
    public void doSomeTesting() {
        new Expectations() {{
            matcher.find();
        }};
    }
}

 

The error I am getting when using the maven-clover2-plugin:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.105 sec <<< FAILURE!
doSomeTesting(MyTest)  Time elapsed: 0.104 sec  <<< ERROR!
java.lang.IllegalArgumentException: Invalid try/finally statement inside expectation block
	at MyTest$1.<init>(MyTest.java)
	at MyTest.__CLR4_0_22y8kiw1r(MyTest.java:16)
	at MyTest.doSomeTesting(MyTest.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
	at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
	at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

 

The same test is working when executed with the Eclipse Clover plugin.

 

I am currently using Maven 3.2.5, JUnit 4.11, JMockit 1.14 and Clover 4.0.2.

3 answers

1 accepted

1 vote
Answer accepted

Hi Selena,

Compilation fails because Clover adds a try-finally block inside the instance initializer block, what looks like this (this is a simplified example, Clover adds much more, but this sample reproduces the same error without Clover):

@Test
public void doSomeTesting() {
    new Expectations() {{
        try {
            matcher.find();
        } finally {

        }
    }};
}

 

JMockit does not expect to have any try/finally inside Expectations' instance initializer. You have to disable instrumentation of such blocks by Clover. You can use ///CLOVER:OFF ///CLOVER:ON keywords (note three slashes), for instance.

 

Example:

@Test
public void doSomeTesting() {
    ///CLOVER:OFF
    new Expectations() {{
        matcher.find();
    }};
    ///CLOVER:ON

    // other code ...
    matcher.find();
}

 

 

Works like a charm, thanks! :)

0 votes

Could you please tell what Java version do you use?

Hi, I tried the ///CLOVER:OFF and ///CLOVER:ON directives, but still saw the error. I tried to exclude using maven plugin config, but somehow still does not work, can you check to see if the regex is not correct?

jmockit version:

 <org.jmockit.version>1.16</org.jmockit.version>

clover2 plugin version:

<maven-clover2-plugin.version>4.0.0</maven-clover2-plugin.version>

 

clover2 plugin configuraiton:

 

<plugin>
                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>maven-clover2-plugin</artifactId>
                <configuration>
                    <statementContexts>
                        <expectations>(.* )?new Expectations\(.*\)\{[\S\s]*\};</expectations>
                    </statementContexts>
                    <contextFilters>expectations</contextFilters>
                </configuration>
            </plugin>

> Hi, I tried the ///CLOVER:OFF and ///CLOVER:ON directives, but still saw the error. Could you paste a code snippet, please?

> I tried to exclude using maven plugin config, but somehow still does not work, can you check to see if the regex is not correct? It does not work in a way as you expect. The <statementContexts> and <methodContexts> do not disable Clover's instrumentation in blocks matching a regular expression. Instead of this, these contexts will record special markers in Clover's database for code sections matching the expression, so that such blocks can be later excluded in reports. Therefore, in order to solve compilation error in JMockit you have to use ///CLOVER:OFF and ///CLOVER:ON markers.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Jira Cloud Performance Improvements

Hello everyone, I am a product manager in the Jira Cloud team focused on making sure our customers have a delightful experience using our products. Towards that goal, one of the areas which is extr...

171 views 2 9
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you