使用注解@Sql,是否可以在Class级别执行脚本,然后再执行Method级别?
Using annotation @Sql, is it possible to execute scripts in Class level before Method level?
我想在每个测试方法之前执行两个脚本,但我也想定义一些脚本在特定方法之前执行。使用Spring框架和@Sql注解,可以吗?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/WEB-INF/application-context.xml")
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
@Sql({ "/database/drop_schema.sql", "/database/create_schema.sql" })
public class CentralServiceTestCase {
// I want to run "drop_schema.sql", "create_schema.sql"
// and "basic_data.sql" here
@Test
@Sql({ "/database/basic_data.sql" })
public void processActionWithSuccess() {
}
// I want to run only "drop_schema.sql" and "create_schema.sql" here
@Test
public void anotherTestMethod() {
}
}
运行这段代码只"basic_data.sql"被执行。如何解决这个问题呢?我将不得不从 class 中删除 @Sql 注释并使用 "/database/drop_schema.sql" 和 "/database/create_schema.sql" 定义?
更新:现在可以通过新的 @SqlMergeMode
注释在即将发布的 Spring Framework 5.2 版本中实现(参见 reference manual)。
@Sql
的 Javadoc 的第 2 段明确指出:
Method-level declarations override class-level declarations.
这也记录在参考手册的 Executing SQL scripts declaratively with @Sql 部分。
因此,不,不幸的是,如果 @Sql
也在方法级别定义,则无法执行在 class 级别通过 @Sql
声明的脚本。
如果您希望 Spring 支持此类组合,请 create a JIRA issue 请求此功能并 select Test
Component.
谢谢!
Sam(Spring TestContext Framework 的作者)
我想在每个测试方法之前执行两个脚本,但我也想定义一些脚本在特定方法之前执行。使用Spring框架和@Sql注解,可以吗?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/WEB-INF/application-context.xml")
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
@Sql({ "/database/drop_schema.sql", "/database/create_schema.sql" })
public class CentralServiceTestCase {
// I want to run "drop_schema.sql", "create_schema.sql"
// and "basic_data.sql" here
@Test
@Sql({ "/database/basic_data.sql" })
public void processActionWithSuccess() {
}
// I want to run only "drop_schema.sql" and "create_schema.sql" here
@Test
public void anotherTestMethod() {
}
}
运行这段代码只"basic_data.sql"被执行。如何解决这个问题呢?我将不得不从 class 中删除 @Sql 注释并使用 "/database/drop_schema.sql" 和 "/database/create_schema.sql" 定义?
更新:现在可以通过新的 @SqlMergeMode
注释在即将发布的 Spring Framework 5.2 版本中实现(参见 reference manual)。
@Sql
的 Javadoc 的第 2 段明确指出:
Method-level declarations override class-level declarations.
这也记录在参考手册的 Executing SQL scripts declaratively with @Sql 部分。
因此,不,不幸的是,如果 @Sql
也在方法级别定义,则无法执行在 class 级别通过 @Sql
声明的脚本。
如果您希望 Spring 支持此类组合,请 create a JIRA issue 请求此功能并 select Test
Component.
谢谢!
Sam(Spring TestContext Framework 的作者)