Closed AlexStasko closed 7 years ago
The problem is due to the new way to manage data provider parameters: https://github.com/cbeust/testng/pull/923
Maybe JMockit should upgrade its TestNG support. But let me know if a specific spi is needed to help with the integration.
Ping @nitinverma who made the 6.9.11 modification, maybe he'll have a good idea for a nice integration.
For reference, we have https://github.com/cbeust/testng/issues/600 in the backlog too which points some integration problems on dataprovider.
TestNG should provide an extensible mechanism for third-party libraries to provide their own custom arguments to test methods, without them having to resort to hacks as I had to do in JMockit. JUnit 5 (not JUnit 4) does provide such a mechanism (the ParameterResolver
extension interface), and it is used in JMockit.
I will see if the new parameter resolution in TestNG 6.9.11+can be supported. From the first look, it may be difficult. I don't exclude the possibility that mock parameters in test methods won't be usable anymore with these newer versions of TestNG; in that case, JMockit users would have to limit themselves to mock fields.
Hi @AlexStasko, @rliesenfeld
testng is expecting an object that is an instance of 'DummyDomain' or null, as both are valid inputs to invoke this test method.
Following snippet would satisfy testng-6.9.11 for now.
@DataProvider(name = "data")
public Object[][] data() {
return new Object[][]{
new Object[]{null},
new Object[]{new DummyDomain("from-data-provider")},
};
}
@Test(dataProvider = "data", expectedExceptions = {RuntimeException.class})
public void testStore(@Mocked DummyDomain unused) {
System.out.println("unused:" + unused);
new StrictExpectations() {
{
new DummyDomain("TEST1");
try {
unused.exec();
} catch (IOException e) {
// never happen
}
result = new IOException();
}
};
dummyService.store("test");
}
Happy to help n collaborate on this integration.
Regards,
Nitin Verma
Refer https://github.com/jmockit/jmockit1
if getInjectedParameter(...)
returns corresponding mock object in place of the empty string. Above test should start working. What additional context would you require to build a mock?
TestNGRunnerDecorator.java
public static final class MockParameters extends MockUp<Parameters>
{
@Mock
public static void checkParameterTypes(
String methodName, Class<?>[] parameterTypes, String methodAnnotation, String[] parameterNames) {}
@Mock
@Nullable
public static Object getInjectedParameter(
@Nonnull Invocation invocation, Class<?> c, @Nullable Method method,
ITestContext context, ITestResult testResult)
{
((MockInvocation) invocation).prepareToProceedFromNonRecursiveMock();
Object value = Parameters.getInjectedParameter(c, method, context, testResult);
if (value != null) {
return value;
}
if (method == null) {
// Test execution didn't reach a test method yet.
return null;
}
if (method.getParameterTypes().length == 0) {
// A test method was reached, but it has no parameters.
return null;
}
if (isMethodWithParametersProvidedByTestNG(method)) {
// The test method has parameters, but they are to be provided by TestNG, not JMockit.
return null;
}
// It's a mock parameter in a test method, to be provided by JMockit.
return "";
}
}
It stopped working for testng 6.12. Reopen?
@Test
public void test(@Injectable Runnable runnable) {
}
org.testng.internal.reflect.MethodMatcherException: Data provider mismatch Method: test([Parameter{index=0, type=java.lang.Runnable, declaredAnnotations=[@mockit.Injectable(value=)]}]) Arguments: []
at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
at org.testng.internal.Invoker.injectParameters(Invoker.java:1301)
I use JMockit 1.27 Java 1.8.92 And Windows OS
Report prints following error:
That problem happens with testng 6.9.11 and higher. With testng 6.9.10 this test runs successfully. I am not sure the problem is in jmockit, but might be this is an issue somewhere in proxy that jmockit creates for method parameter.
Test project is here jmockit-check.zip
Thank you, Alex