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
Accepted answer

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
Community showcase
Posted Dec 10, 2018 in Confluence

Organizing your space just got easier - Page Tree Drag & Drop is here

Hi Community! I’m Elaine, Confluence Product Manager. You may have read my earlier post about page tree in space navigation sidebar. I'm excited to share another improvement that helps you organize ...

203 views 6 7
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you