如何在 spring 启动测试中打印 @sql 注释中脚本文件的完整路径
How to print full path to script files in @sql annotation in spring boot test
在多模块项目中,我想确保 Spring 的 @sql 注释使用正确的资源。有没有办法以某种方式记录这些文件的完整路径以进行控制台?
Spring 在执行前记录脚本文件名,但在测试不同模块时,这些文件名有时是相同的。
SqlScriptsTestExecutionListener
- 负责@Sql
的处理,第一步可以通过添加属性 logging.level.org.springframework.test.context.jdbc=debug
更改为调试相关日志,但是调试信息不完整,如果还不够,您应该创建自己的 TestExecutionListener
并在测试中声明 class @TestExecutionListeners(listeners = SqlScriptsCustomTestExecutionListener.class)
例如:
public class SqlScriptsCustomTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void beforeTestMethod(TestContext testContext) {
List<Resource> scriptResources = new ArrayList<>();
Set<Sql> sqlAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(testContext.getTestMethod(), Sql.class);
for (Sql sqlAnnotation : sqlAnnotations) {
String[] scripts = sqlAnnotation.scripts();
scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
scriptResources.addAll(TestContextResourceUtils.convertToResourceList(testContext.getApplicationContext(), scripts));
}
if (!scriptResources.isEmpty()) {
String debugString = scriptResources.stream().map(r -> {
try {
return r.getFile().getAbsolutePath();
} catch (IOException e) {
System.out.println("Unable to found file resource");
}
return null;
}).collect(Collectors.joining(","));
System.out.println(String.format("Execute sql script :[%s]", debugString));
}
}
这只是一个简单的例子,它可以工作。我从 SqlScriptsTestExecutionListener
复制的大部分源代码只是为了解释。它只是在方法级别 @Sql
注释的情况下实现,而不包括 class 级别。
希望对你有帮助。
在多模块项目中,我想确保 Spring 的 @sql 注释使用正确的资源。有没有办法以某种方式记录这些文件的完整路径以进行控制台? Spring 在执行前记录脚本文件名,但在测试不同模块时,这些文件名有时是相同的。
SqlScriptsTestExecutionListener
- 负责@Sql
的处理,第一步可以通过添加属性 logging.level.org.springframework.test.context.jdbc=debug
更改为调试相关日志,但是调试信息不完整,如果还不够,您应该创建自己的 TestExecutionListener
并在测试中声明 class @TestExecutionListeners(listeners = SqlScriptsCustomTestExecutionListener.class)
例如:
public class SqlScriptsCustomTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void beforeTestMethod(TestContext testContext) {
List<Resource> scriptResources = new ArrayList<>();
Set<Sql> sqlAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(testContext.getTestMethod(), Sql.class);
for (Sql sqlAnnotation : sqlAnnotations) {
String[] scripts = sqlAnnotation.scripts();
scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
scriptResources.addAll(TestContextResourceUtils.convertToResourceList(testContext.getApplicationContext(), scripts));
}
if (!scriptResources.isEmpty()) {
String debugString = scriptResources.stream().map(r -> {
try {
return r.getFile().getAbsolutePath();
} catch (IOException e) {
System.out.println("Unable to found file resource");
}
return null;
}).collect(Collectors.joining(","));
System.out.println(String.format("Execute sql script :[%s]", debugString));
}
}
这只是一个简单的例子,它可以工作。我从 SqlScriptsTestExecutionListener
复制的大部分源代码只是为了解释。它只是在方法级别 @Sql
注释的情况下实现,而不包括 class 级别。
希望对你有帮助。