spring 如何在@Before 方法之前执行@Sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27126974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:13:08  来源:igfitidea点击:

How to execute @Sql before a @Before method

springspring-test

提问by Rafael Duque Estrada

I'm trying to combine the follow annotations:

我正在尝试结合以下注释:

org.springframework.test.context.jdbc.Sqland org.junit.Before

org.springframework.test.context.jdbc.Sqlorg.junit.Before

Like the follow code:

像下面的代码:

@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");
}

The code in the method @Before is running after then the script "dml-parametro.sql" in the @Sql annotation.

方法@Before 中的代码在@Sql 注释中的脚本“dml-parametro.sql”之后运行。

Is it right to do this?

这样做是否正确?

For solution this, I'm using @After in place than @Before, but I'd like to cdelete tables before the test execution, not after.

为了解决这个问题,我在原地使用@After而不是@Before,但我想在测试执行之前而不是之后cdelete表。

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.

我不想使用@SqlConfig。我没有在测试级别使用事务范围,所以我需要在每个测试方法中清理我的表。如果每个测试方法都需要清理表,我想在@Before 方法中执行此操作。我不想在每个带有@SqlConfig 的测试方法中都这样做。我认为 @Sql 比 @Before 之前执行的行为是错误的。

回答by Sam Brannen

By default, any SQL scripts executed via @Sqlwill be executed beforeany @Beforemethods. So the behavior you are experiencing is correct, but you can change the execution phase via the executionPhaseattribute in @Sql(see example below).

默认情况下,任何通过 via@Sql执行的SQL 脚本都将任何@Before方法之前执行。因此,您遇到的行为是正确的,但您可以通过executionPhasein 属性更改执行阶段@Sql(请参见下面的示例)。

If you want to execute multiple scripts, that is also possible via @Sql.

如果要执行多个脚本,也可以通过@Sql.

So if you have a clean-up script named clean-parametro.sqlthat deletes from the PARAMETROtable, you could annotate your test method like the following (instead of invoking JdbcTestUtils.deleteFromTables()in your @Beforemethod).

因此,如果您有一个名为clean-parametro.sqlPARAMETRO表中删除的清理脚本,您可以像下面这样注释您的测试方法(而不是JdbcTestUtils.deleteFromTables()在您的@Before方法中调用)。

@Test
@Sql({"dml-parametro.sql", "clean-parametro.sql"})
public void test() { /* ... */ }

Of course, if dml-parametro.sqlinserts values into the PARAMETROtable, then it likely does not make sense to immediately delete those values in the clean-up script.

当然,如果dml-parametro.sql将值插入到PARAMETRO表中,那么在清理脚本中立即删除这些值可能没有意义。

Please note that @Sqland @SqlConfigprovide multiple levels of configuration for script execution.

请注意,@Sql@SqlConfig为脚本执行提供多级配置。

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:

例如,如果您想在测试之前创建表并在测试之后进行清理,您可以在 Java 8 上执行以下操作:

@Test
@Sql("create-tables.sql")
@Sql(scripts = "clean-up.sql", executionPhase = AFTER_TEST_METHOD)
public void test() { /* ... */ }

Or use @SqlGroupas a container on Java 6 or Java 7:

或者@SqlGroup用作 Java 6 或 Java 7 上的容器:

@Test
@SqlGroup({
    @Sql("create-tables.sql"),
    @Sql(scripts = "clean-up.sql", executionPhase = AFTER_TEST_METHOD)
})
public void test() { /* ... */ }

If your tests are @Transactionaland 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:

如果您的测试是@Transactional并且您想清理已提交的数据库状态,则可以指示 Spring 在新事务中执行清理 SQL 脚本,如下所示:

@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!

我希望这可以为您澄清事情!

Cheers,

干杯,

Sam (author of the Spring TestContext Framework)

Sam (Spring TestContext 框架的作者)



Notes:

笔记:

  • AFTER_TEST_METHODis statically imported from ExecutionPhase
  • ISOLATEDis statically imported from TransactionMode
  • AFTER_TEST_METHOD从静态导入 ExecutionPhase
  • ISOLATED从静态导入 TransactionMode