admin管理员组

文章数量:1307167

Our Spring Boot application modifies classes by adding certain annotations (annotateType(...)) with ByteBuddy. For example:

private DynamicType.Loaded<? extends MyClassToRedefine> getLoadedTypeOfResource() {
    if (myClassToRedefineImplLoadedType == null) {
            try (var unloadedResource = new ByteBuddy()
                    .subclass(MyClassToRedefine.class)
                    .annotateType(newAnnotation1)
                    .annotateType(newAnnotation2)
                    .annotateType(newAnnotation3)
                    .make()) {
             myClassToRedefineImplLoadedType = unloadedResource.load(ClassLoader.getSystemClassLoader());
            }
    }
    return myClassToRedefineImplLoadedType;

}

The Spring Boot tests already have @DirtiesContext. They run fine individually.

However, when run together I notice that the annotations stay the same between tests which causes issues. The root cause is that ByteBuddy modifies classes at the JVM level, and those modifications persist across multiple test runs.

How can I clear/reset all modifications between tests?

EDIT: used annotations in test:

@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
@EnableKubeAPIServer(updateKubeConfigFile = true)
@DirtiesContext

本文标签: javaReset ByteBuddy modified classes between Spring Boot integration testsStack Overflow