集成测试之前如何启动sql-script?

时间:2018-10-23 09:12:03

标签: spring

spring + oracle上有应用程序。 因此,我需要在集成测试之前创建sql-before脚本来清理基础。

所以,我的IT看起来像这样

@Category(Integrations.class)
@WebAppConfiguration
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:application-${envConfig}.properties")
@ContextConfiguration(classes = {IntegrationConfig.class}, loader = AnnotationConfigWebContextLoader.class)
@Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:before.sql")
public class ControllerTest {
    @Value("${server.remote}")
    private String remote;

    @Value("${app.name}")
    private String appName;
    private final String HTTP = "http://";

    protected RestTemplate restTemplate = new RestTemplate();

    @Value("${server.port}")
    protected int port;

    @Value("${server.address}")
    protected String serverAddress;

    @Test
    public void test(){
    }

envConfig-是来自jv的环境变量。 在test->使用我的脚本的资源命名文件before.sql中。

尝试启动此测试时出现错误消息:

 at org.springframework.util.Assert.state(Assert.java:94)
        at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.executeSqlScripts(SqlScriptsTestExecutionListener.java:203)
        at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.executeSqlScripts(SqlScriptsTestExecutionListener.java:142)
        at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.afterTestMethod(SqlScriptsTestExecutionListener.java:121)
        at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:443)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:94)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      ...
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.04 sec <<< FAILURE!
test(integration.ControllerTest)  Time elapsed: 0.016 sec  <<< ERROR!
java.lang.IllegalStateException: Failed to execute SQL scripts for test context [DefaultTestContext@16a0ee18 testClass = ControllerTest, testInstance = integration.RiskMet
ricControllerTest@3d6f0054, testMethod = test@ControllerTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@505fc5a4 testClass = Risk
MetricControllerTest, locations = '{}', classes = '{class integration.config.IntegrationConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations =
'{classpath:application-${envConfig}.properties}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], resourceBasePath = 'src/main/webapp', contextLoader = 'org.spr
ingframework.test.context.web.AnnotationConfigWebContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.populatedReque
stContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]: supply at least a DataSource or PlatformTransactio
nManager.
        at org.springframework.util.Assert.state(Assert.java:94)
        at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.executeSqlScripts(SqlScriptsTestExecutionListener.java:203)
        at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.executeSqlScripts(SqlScriptsTestExecutionListener.java:142)
        at org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener.afterTestMethod(SqlScriptsTestExecutionListener.java:121)
        at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:443)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:94)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:27)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
        at org.apache.maven.surefire.junitcore.JUnitCoreWrapper.execute(JUnitCoreWrapper.java:62)
        at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:139)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Tests in error:
  test(integration.ControllerTest): Failed to execute SQL scripts for test context [DefaultTestContext@16a0ee18 testClass = ControllerTest, testInstance = integr
ation.ControllerTest@3d6f0054, testMethod = test@ControllerTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@505fc5a4 tes
tClass = ControllerTest, locations = '{}', classes = '{class integration.config.IntegrationConfig}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourc
eLocations = '{classpath:application-${envConfig}.properties}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], resourceBasePath = 'src/main/webapp', contextLoad
er = 'org.springframework.test.context.web.AnnotationConfigWebContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.p
opulatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]: supply at least a DataSource or Platf
ormTransactionManager.

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

1 个答案:

答案 0 :(得分:0)

您可以使用 flyway DbUnit 之类的库。 在我们的项目中,我们有一个自定义的gradle插件,可以在测试任务运行之前设置flyway脚本。

对于您来说,这很简单,例如 flyway-test-extension 。是的,您必须了解飞行路线,但最多只需阅读一个小时即可。

https://github.com/flyway/flyway-test-extensions

所以飞行测试的想法是这样的:

  • 提及要测试的位置中的测试脚本
  • 在您的单元测试方法上使用@Before,例如@AfteruseUnicode=true&characterEncoding=utf-8