bundle中的Jacoco类与事务方法不匹配

问题描述 投票:4回答:1

我正在使用JUnit5测试应用程序,并使用Jacoco进行覆盖率报告。执行测试,确定并提供测试报告。

但是,如果服务包含用@Transactional注释的方法,则Jacoco报表具有以下日志:>

[ant:jacocoReport] Classes in bundle 'my-service' do no match with execution data. For report generation the same class files must be used as at runtime.
[ant:jacocoReport] Execution data for class mypackage/SampleService does not match. 

此错误对于所有@Service类方法都发生,并用@Transactional进行注释,可以计算出普通类的覆盖率。

这里是样本测试:

@SpringBootTest
@ExtendWith(SpringExtension.class)
public class MyServiceTest {

    @Autowired
    private SampleService sampleService;

    @Test
    public void doWork(){
        sampleService.doWork();
    }
}

效果很好。覆盖范围非零:

public class SampleService {

    public void doWork(){
        System.out.println("HEY");
    }
}

0%覆盖率:

public class SampleService {

    @Transactional
    public void doWork(){
        System.out.println("HEY");
    }
}

事务处理围绕实际类创建代理。但是,Jacoco是否有一种开箱即用的方式来处理这种常见情况?

我尝试了@EnableAspectJAutoProxy注释,并带有不同的标志变化,检查是否使用了最新的Jupiter引擎和Jacoco插件

这里是gradle配置:

subprojects {
    test {       
        useJUnitPlatform()
    }

    jacocoTestReport {
        afterEvaluate {
            classDirectories.from = files(classDirectories.files.collect {
                fileTree(dir: it, exclude: '*Test.java')
            })
        }

        reports {
            html.enabled = true
            xml.enabled = true
            csv.enabled = false
        }
    }
}

任何帮助表示感谢

我正在使用JUnit5测试应用程序,并使用Jacoco进行覆盖率报告。执行测试,确定并提供测试报告。但是,如果服务包含方法,则Jacoco报表具有以下日志,...

java spring spring-boot gradle jacoco
1个回答
3
投票

我在一个测试项目中进行了尝试,类似于您所描述的,但是我无法重现该问题。我在您的项目和我的项目之间看到的唯一区别是,我使用了maven而不是gradle。

© www.soinside.com 2019 - 2024. All rights reserved.