Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State associated with @Tested class + @Inject constructor doesn't appear to be cleaned up between test classes #258

6bangs opened this issue Feb 19, 2016 · 2 comments


Copy link

@6bangs 6bangs commented Feb 19, 2016

Minimum example:

@Suite.SuiteClasses({ Test1.class, Test2.class })
public class TwoTests {}

public class A {
    public A(Double dep) {}

    public void doSomething(Boolean parameter) {}

public class Test1 {
    @Tested A a;
    @Injectable Double dep;

    public void testOne() throws Exception {}

public class Test2 {
    @Mocked A a;

    public void testTwo() throws Exception {
        new Verifications() {{
            a.doSomething(anyBoolean); times = 0;

If Test1 is run before Test2, an NPE is raised with the following stack trace:

    at mockit.internal.state.ParameterNames.getName(
    at mockit.internal.util.MethodFormatter.appendParameterName(
    at mockit.internal.util.MethodFormatter.appendFriendlyTypes(
    at mockit.internal.util.MethodFormatter.appendFriendlyMethodSignature(
    at mockit.internal.util.MethodFormatter.<init>(
    at mockit.internal.expectations.invocation.InvocationArguments.toString(
    at mockit.internal.expectations.invocation.ExpectedInvocation.toString(
    at java.lang.String.valueOf(
    at java.lang.StringBuilder.append(
    at mockit.internal.expectations.invocation.ExpectedInvocation.errorForMissingInvocation(
    at mockit.internal.expectations.BaseVerificationPhase.handleInvocation(
    at mockit.internal.expectations.RecordAndReplayExecution.recordOrReplay(
    at A.doSomething(
    at Test2$1.<init>(
    at Test2.testTwo(

However, if Test2 is run first, both tests pass.

JMockit version: 1.21
JUnit version: 4.11

@rliesenfeld rliesenfeld self-assigned this Feb 19, 2016
Copy link

@rliesenfeld rliesenfeld commented Feb 19, 2016

Thanks! It's a bug indeed, and not limited to multiple tests; the simplified test below also reproduces it:

public class ParameterNameExtractionTest
    static class A
        A() {}
        A(int i) {}
        void doSomething(boolean b) {}

    @Tested A a1; // causes parameter name extraction for class A

    public void mockClassPreviouslyUsedInTestedField(@Mocked final A a2)
        new Verifications() {{
            a2.doSomething(true); // NPE here, as extraction missed "doSomething(boolean)"
            times = 0;

Copy link

@6bangs 6bangs commented Feb 19, 2016

Ahh, glad to get a peek at what's going on under the covers :)

I debugged far enough to understand that @tested caused parameter name extraction, but didn't have time yesterday to dig deeper. Looking forward to reading the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

No branches or pull requests

2 participants