SqlScriptsTestExecutionListener
- отвечает за обработку @Sql
, для первого шага вы можете перейти к отладочному связанному журналу, добавив свойство logging.level.org.springframework.test.context.jdbc=debug
, но сообщение отладки не полностью, и если его недостаточно, вы должны создать свойвладеть TestExecutionListener
и объявить на тестовом классе @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
на уровне метода, а не на уровне класса.Я надеюсь, что это поможет вам.