Spring Boot和dbunit测试

时间:2018-10-12 11:34:15

标签: spring spring-boot dbunit spring-test-dbunit

我正在尝试为我的spring boot(+ Apache骆驼)gradle项目编写dbunit测试,以确保持久性后我的数据库具有预期的数据。

我的build.gradle:

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
        camelVersion = '2.22.1'
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("io.franzbecker:gradle-lombok:1.14")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.dummy'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir

dependencies {
    compile 'org.apache.camel:camel-spring-boot-starter:2.22.0'
    compile "org.apache.camel:camel-restlet:$camelVersion"
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:2.0.5.RELEASE'
    compile 'org.hibernate:hibernate-core:5.3.5.Final'
    compile 'com.oracle:ojdbc8:12.2.0.1'

    testCompile "org.apache.camel:camel-test-spring:$camelVersion"
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'com.github.springtestdbunit:spring-test-dbunit:1.2.0'
    testCompile 'org.dbunit:dbunit:2.5.0'

}

我的测试班:

@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class})
@Sql({ "/scripts/cleanup.sql", "/scripts/setup.sql" }) //never gets executed
public class EdiInvoiceITSpringDbunit {
    private static final String GET_TEST_INVOICES_SQL = "SELECT COUNT(*) FROM EDI_INVOICE_HEAD";
    private Exchange exch;
    private Message message;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private CamelContext camelContext;

    @Before
    public void testSetup() {
        exch = new DefaultExchange(camelContext);
        message = exch.getIn();
        message.setHeader("mYHeader", "doesn't matter");
    }

    @Test
    //this @ExpectedDatabase annotation does practically nothing
    @ExpectedDatabase(value="/myExpectedTestData.xml", assertionMode=DatabaseAssertionMode.NON_STRICT)
    public void MyTest() throws Exception {
        int resultCount = jdbcTemplate.queryForObject(GET_TEST_INVOICES_SQL, Integer.class);
        assertEquals(resultCount, 0); //fails here because clean scripts are never executed

        //My logic that calls my service and Dao and does the persistence.

        resultCount = jdbcTemplate.queryForObject(GET_TEST_INVOICES_SQL, Integer.class);
        assertEquals(resultCount, 1);
    }

}

@ExpectedDatabase注释完全不执行任何操作。不幸的是,当我使用spring-dbunit时,即使@SQL注释也不起作用。

完全不确定可能会丢失什么。

0 个答案:

没有答案