Clover / JMockit: invalid try/finally in expectations block

Selena Lowell February 16, 2015

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
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 16, 2015

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

 

 

Selena Lowell February 17, 2015

Works like a charm, thanks! :)

0 votes
Guozheng Ge April 1, 2015

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>
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 7, 2015

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

Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 7, 2015

> 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.

0 votes
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 16, 2015

Could you please tell what Java version do you use?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events