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


None yet

2 participants

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 added the bug label Feb 19, 2016
@rliesenfeld rliesenfeld self-assigned this 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;
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!

@rliesenfeld rliesenfeld added a commit that closed this issue Feb 21, 2016
@rliesenfeld rliesenfeld Fixed bug where parameter name extraction was performed only for cons…
…tructors, but needed for methods as well; closes #258.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment