Весенние тесты загрузки и dbunit - PullRequest
0 голосов
/ 12 октября 2018

Я пытаюсь написать тесты dbunit для моего проекта gradle с весенней загрузкой (+ apache camel), чтобы убедиться, что в моей базе данных есть ожидаемые данные после сохранения.

Мой 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 просто ничего не делает.К сожалению, даже аннотация @SQL не работает, когда я использую spring-dbunit.

Совершенно неуверен в том, что, возможно, отсутствует.

...