问题描述
我正在尝试合并以下注释:
I'm trying to combine the follow annotations:
org.springframework.test.context.jdbc.Sql 和 org.junit.之前
就像下面的代码一样:
@Test
@Sql(scripts = "dml-parametro.sql")
public void testData(){
Iterable<Parametro> parametros = parametroService.findAll();
List<Parametro> parametrosList = Lists.newArrayList(parametros);
Assert.assertThat(parametrosList.size(), Is.is(1));
}
@Before
public void beforeMethod() {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "PARAMETRO");
}
@Before方法中的代码在@Sql批注中的脚本"dml-parametro.sql"之后运行.
The code in the method @Before is running after then the script "dml-parametro.sql" in the @Sql annotation.
这样做正确吗?
对于此解决方案,我使用@After而不是@Before,但我想在测试执行之前而不是之后cdelete表.
For solution this, I'm using @After in place than @Before, but I'd like to cdelete tables before the test execution, not after.
我不想使用@SqlConfig.我没有在测试级别上使用Transacional范围,因此我需要在每种测试方法中清除表.如果每个测试方法都需要清理表,我想在@Before方法中做到这一点.我不想在每个使用@SqlConfig的测试方法中执行此操作.我认为@Sql要比@Before早执行的行为是错误的.
I wouldn't like to use @SqlConfig. I'm not using transacional scope on test level, so i need to clean my tables in every test method. If every test method need to clean tables, i would like to do this in @Before method. I wouldn't like to do this in every test method with @SqlConfig. I think the behavior of @Sql to be execute before than @Before is wrong.
推荐答案
默认情况下,通过@Sql
执行的所有SQL脚本都将在 任何@Before
方法之前执行.因此,您遇到的行为是正确的,但是您可以通过@Sql
中的executionPhase
属性更改执行阶段(请参见下面的示例).
By default, any SQL scripts executed via @Sql
will be executed before any @Before
methods. So the behavior you are experiencing is correct, but you can change the execution phase via the executionPhase
attribute in @Sql
(see example below).
如果要执行多个脚本,也可以通过@Sql
来实现.
If you want to execute multiple scripts, that is also possible via @Sql
.
因此,如果您有一个名为clean-parametro.sql
的清理脚本从PARAMETRO
表中删除,则可以像下面这样注释测试方法(而不是在@Before
方法中调用JdbcTestUtils.deleteFromTables()
)./p>
So if you have a clean-up script named clean-parametro.sql
that deletes from the PARAMETRO
table, you could annotate your test method like the following (instead of invoking JdbcTestUtils.deleteFromTables()
in your @Before
method).
@Test
@Sql({"dml-parametro.sql", "clean-parametro.sql"})
public void test() { /* ... */ }
当然,如果dml-parametro.sql
将值插入PARAMETRO
表中,那么立即删除清理脚本中的这些值可能就没有意义.
Of course, if dml-parametro.sql
inserts values into the PARAMETRO
table, then it likely does not make sense to immediately delete those values in the clean-up script.
请注意,@Sql
和@SqlConfig
为脚本执行提供了多个配置级别.
Please note that @Sql
and @SqlConfig
provide multiple levels of configuration for script execution.
例如,如果要在测试之前创建表并在测试之后进行清理,则可以在Java 8上执行以下操作:
For example, if you want to create tables before your test and clean up after your test, you could do something like this on Java 8:
@Test
@Sql("create-tables.sql")
@Sql(scripts = "clean-up.sql", executionPhase = AFTER_TEST_METHOD)
public void test() { /* ... */ }
或将@SqlGroup
用作Java 6或Java 7上的容器:
Or use @SqlGroup
as a container on Java 6 or Java 7:
@Test
@SqlGroup({
@Sql("create-tables.sql"),
@Sql(scripts = "clean-up.sql", executionPhase = AFTER_TEST_METHOD)
})
public void test() { /* ... */ }
如果测试为@Transactional
,并且您想要清除已提交的数据库状态,则可以指示Spring在新事务中执行清除SQL脚本,如下所示:
If your tests are @Transactional
and you'd like to clean up committed database state, you can instruct Spring to execute your clean-up SQL script in a new transaction like this:
@Test
@Sql("insert-test-data.sql")
@Sql(
scripts = "clean-up.sql",
executionPhase = AFTER_TEST_METHOD,
config = @SqlConfig(transactionMode = ISOLATED)
)
public void test() { /* ... */ }
我希望这可以为您澄清一切!
I hope this clarifies things for you!
干杯
Sam (Spring TestContext Framework的作者)
注意:
-
AFTER_TEST_METHOD
是从ExecutionPhase
静态导入的 -
ISOLATED
是从TransactionMode
静态导入的
AFTER_TEST_METHOD
is statically imported fromExecutionPhase
ISOLATED
is statically imported fromTransactionMode
这篇关于如何在@Before方法之前执行@Sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!