Open DanskerDave opened 4 years ago
import org.slf4j.*;
import mockit.*;
public class MockToString {
public static final class MyClass {
private final String value;
private MyClass(final String value) {
this.value = value;
}
public final byte[] array() {
return this.value.getBytes();
}
@Override
public final String toString() {
return this.value;
}
}
private static final Logger LOG = LoggerFactory.getLogger(MockToString.class);
public static final class MyClassFaked extends MockUp<MyClass> {
@Mock
public Object $advice(final Invocation invocation) {
LOG.info("$advice.......: {} {} {}", invocation.getInvocationIndex(), invocation.getInvokedMember(), invocation.getInvokedInstance());
return invocation.proceed();
}
}
private static final MyClass real = new MyClass("ABC");
private static MyClass fake = new MyClass("Fake!!");
public static void main(final String[] args) throws Exception {
LOG.info("Real..........: {} {}", real, real.array());
LOG.info("Fake..........: {} {}", fake, fake.array());
LOG.info("MockUp........: {}", new MyClassFaked());
LOG.info("new Fake()....: {}", fake = new MyClass("DEF"));
LOG.info("Real.toString.: {}", real.toString(), real.array(/* resolved 2nd (not logged) */), real.toString());
LOG.info("Fake.toString.: {}", fake.toString(), fake.array(/* resolved 2nd (not logged) */), fake.toString());
}
}
When faking a Class, the toString() method is not made available to the $advice method.
I think I understand why, but if its intentional, please document that fact.
Are any other methods suppressed?
Please see attached example.
JDK: openJDK 8u40 JMockit v1.49 MockToString.java.txt