[我有一个正在使用的相当新的Spring Boot Java应用程序,但我没有意识到默认情况下没有启用spring jpa“ open-in-view”设置。
我遇到了一些问题,这些问题导致禁用该设置最终是最好的解决方案,但是现在我遇到了一些偶然地依赖它的情况。
例如,我有一个未标记为@Transactions的Service类,在返回到控制器之前,在我的一个实体的惰性初始化子级上进行了循环。
现在,此函数按预期方式引发LazyInitializationException,因为我不在事务中。
我尚未编写可提供100%覆盖率的测试用例,是否可以在Eclipse或Sprint Tools Suite或Spring Boot运行时中内置任何可生成报告的内容,以告诉我何时使用@Entity注释的类在非事务方法调用中对它们进行过函数调用吗?
[尝试以其他方式尝试识别此问题,而无需在我的应用程序中添加100%代码覆盖率测试,还是用细齿梳检查每个方法调用?
如果没有,那么我将处理测试用例。
感谢您的指导。
这并不完美,但是我确实找到了一种使用Reflections
库为我的应用程序查找大多数案例的方法。我将其作为@SpringBootTest
测试用例运行,并一次修复了一个错误。
@Test
public void testOne() throws NoSuchMethodException, SecurityException{
Reflections reflections = new Reflections("my.base.package",
new SubTypesScanner(),
new TypeAnnotationsScanner(),
new MemberUsageScanner());
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(javax.persistence.Entity.class);
for( Class<?> c: annotated){
log.debug(c.getSimpleName());
for( Method m: c.getMethods()){
if( m.getReturnType().isAnnotationPresent(Entity.class) ){
checkTransactional(reflections,c, m, 0);
}
}
}
Assert.assertTrue(true);
}
public static void checkTransactional(Reflections reflections, Class<?> c, Method m, int depth) throws NoSuchMethodException, SecurityException{
if( depth >64){
throw new RuntimeException("No Transactional Method Found");
//return;
}
String s = "--";
Annotation t = null;
Class<?>[] possibleAnnotations = {
org.springframework.transaction.annotation.Transactional.class,
javax.transaction.Transactional.class,
};
Annotation[] annotations = m.getAnnotations();
for( Annotation a: annotations){
Class<?> aClass = a.annotationType();
//// handle javax and spring Transactional
if( aClass.getSimpleName().equals("Transactional")){
t = a;
s = "++";
break;
}
}
String prefix = "";
for( int i = 0; i < depth; i++){
prefix = prefix + s;
}
log.debug( prefix + " " + c.getCanonicalName() + ":" + m.getName());
if( t != null ){
log.trace("This is transactional");
return;
}
Set<Member> callingMembers = reflections.getMethodUsage(m);
if( callingMembers.size() == 0){
log.error("Nothing calls this method, if it is a controller, we have a problem" + (c.isAnnotationPresent(Controller.class) || c.isAnnotationPresent(RestController.class)));
if( (c.isAnnotationPresent(Controller.class) || c.isAnnotationPresent(RestController.class)) ){
throw new RuntimeException("No transactional method found in call heirrchy before controller");
}
}
for( Member usingMember: callingMembers){
Class<?> callingClass = usingMember.getDeclaringClass();
List<Method> callingMethods = new ArrayList<Method>();
if( callingClass != c ){
for( Method callingClassMethod: callingClass.getMethods()){
if( callingClassMethod.getName().equals(usingMember.getName())){
callingMethods.add(callingClassMethod);
}
}
}
if( callingMethods.size() > 2){
log.error("Two methods call this, manually review");
for( Method caller: callingMethods){
log.debug( prefix + "!! " + c.getCanonicalName() + ":" + caller.getName() + ":" + caller.getParameterCount());
}
throw new RuntimeException("Two methods with same name ( overloading ) call this, manually review");
}
for( Method caller: callingMethods){
checkTransactional(reflections, callingClass, caller, depth+1);
}
}
}