I understood. You need to implement your own ClassLoader , which performs the same transformations with the test as ClassFileTransformer (for example, calls it). And, of course, the object class may not have already been loaded, so it cannot be used directly. Therefore, I used the Java reflection API to execute object class methods.
In a separate file:
public static class Subject { public void doSomething(){...} }
In the test:
private static class TransformingClassLoader extends ClassLoader { private final String className; public TransformingClassLoader(String className) { super(); this.className = className; } @Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (name.equals(className)) { byte[] byteBuffer = instrumentByteCode(fullyQualifiedSubjectClass); return defineClass(className, byteBuffer, 0, byteBuffer.length); } return super.loadClass(name); } } @Test public void testSubject(){ ClassLoader classLoader = new TransformingClassLoader(fullyQualifiedSubjectClass); Class<?> subjectClass = classLoader.loadClass(fullyQualifiedSubjectClass); Constructor<?> constructor = subjectClass.getConstructor(); Object subject = constructor.newInstance(); Method doSomething = subjectClass.getMethod("doSomething"); doSomething.invoke(subject); Assert.assertTrue(MyClassFileTransformer.wasCalled()); }
source share