jmockit / jmockit1

Advanced Java library for integration testing, mocking, faking, and code coverage
Other
465 stars 240 forks source link

@Capturing does not work as intended if used as a parameter instead of as a field (TestNG-only) #515

Closed jotunacorn closed 6 years ago

jotunacorn commented 6 years ago

Version of JMockit that was used:

Using JMockit 1.38 and TestNG 6.14.3

Description of the problem:

When passing an @Capturing parameter the test changes behaviour from when the @Capturing is a member. The @Capturing instance does not mock the implementation of the interface when passed as a parameter. Below is a minimal class that works when the interface A is a member but not when it's a parameter.

Test.java

import mockit.Capturing;
import mockit.Expectations;

import static org.testng.Assert.assertEquals;

@org.testng.annotations.Test
public class Test {

    @Capturing A a; //Remove this line and uncomment the parameter for the test to fail.

    public void test(/*@Capturing A a*/) {
        AImpl impl = new AImpl();
        new Expectations() {{
            a.mockMethod();
            result = "test";
        }};
        assertEquals(impl.mockMethod(), "test");
    }

    private interface A {
        String mockMethod();
    }

    private class AImpl implements A {
        public String mockMethod() {
            return "Error";
        }
    }
}

Build.gradle

group 'TestNG'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile "org.testng:testng:6.14.3"
    testCompile "org.jmockit:jmockit:1.38"
}
yasserg commented 6 years ago

We spent a lot of time on updating our tests to be able to upgrade from 1.21 to latest version and now because of this bug can't upgrade. Is this going to be fixed soon and if not is there a workaround? (note that I also tried versions prior to 1.38 and each of those was failing because of other bugs (some related to interactions with TestNG) and couldn't find a recent version that was passing all tests.

rliesenfeld commented 6 years ago

Support for mock test method parameters had to be dropped, as it was no longer viable. If https://github.com/cbeust/testng/issues/1164 ever gets solved, it will be added back.