You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description of the problem or enhancement request:
Per the Mock annotation documentation: "Alternatively, a single fake method having only the Invocation parameter will match all real methods of the same name, regardless of their parameters."
This does not hold true for constructors when faking via $init. I.e., public void $init(Invocation inv) will only match the zero-parameter constructor, not those having parameters.
Check the following:
If a defect or unexpected result, JMockit project members should be able to reproduce it.
For that, include an example test (perhaps accompanied by a Maven/Gradle build script) which
can be executed without changes and reproduces the failure.
Sample code:
`// class being faked
public class MyClass {
private int x;
public MyClass() {
x = 0;
}
public MyClass(int x) {
this.x = x;
}
}`
`// test class
class TestInit { @test
void test() {
var fakedMyClass = new MockUp<MyClass>() {
boolean calledZeroParam = false;
boolean calledOneParam = false;
@Mock
public void $init(Invocation inv) {
if (inv.getInvokedArguments().length == 0) {
calledZeroParam = true;
}
if (inv.getInvokedArguments().length == 1) {
calledOneParam = true;
}
inv.proceed();
}
};
MyClass zeroParam = new MyClass();
MyClass oneParam = new MyClass(42);
assertAll(
() -> assertTrue(fakedMyClass.calledZeroParam),
() -> assertTrue(fakedMyClass.calledOneParam) // <- fails here
);
}
}
`
The JDK where the problem occurs is a final release, not a development build.
JDK Info:
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment Temurin-17.0.5+8 (build 17.0.5+8)
OpenJDK 64-Bit Server VM Temurin-17.0.5+8 (build 17.0.5+8, mixed mode)
The text was updated successfully, but these errors were encountered:
I would have already done that, except I have a situation where I don't know beforehand what the constructor signatures will be. According to the docs, I should be able to intercept all constructors without specifying their formal parameters. I can already do so with any named method, and in fact for all methods (except constructors) with $advice().
You should provide arguments for $init after Invocation with the same type of arguments in target constructor. like this
Please provide the following information:
Version of JMockit that was used: 1.49
Description of the problem or enhancement request:
Per the Mock annotation documentation: "Alternatively, a single fake method having only the Invocation parameter will match all real methods of the same name, regardless of their parameters."
This does not hold true for constructors when faking via
$init
. I.e.,public void $init(Invocation inv)
will only match the zero-parameter constructor, not those having parameters.For that, include an example test (perhaps accompanied by a Maven/Gradle build script) which
can be executed without changes and reproduces the failure.
Sample code:
`// class being faked
public class MyClass {
private int x;
}`
`// test class
class TestInit {
@test
void test() {
}
`
JDK Info:
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment Temurin-17.0.5+8 (build 17.0.5+8)
OpenJDK 64-Bit Server VM Temurin-17.0.5+8 (build 17.0.5+8, mixed mode)
The text was updated successfully, but these errors were encountered: