vendredi 29 janvier 2016

Data not being retrieved from embedded H2 datasource in springboot test

I've used this technique before and specifying all the configuration explicitly. The log indicates it's creating the datasource and loading the scripts:

o.s.j.d.e.EmbeddedDatabaseFactory - Starting embedded database: url='jdbc:h2:mem:balancer;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
o.s.j.d.i.ScriptUtils - Executing SQL script from class path resource [db/migration/V1_0001__create_queue_server_table.sql]
o.s.j.d.i.ScriptUtils - Executed SQL script from class path resource [db/migration/V1_0001__create_queue_server_table.sql] in 20 ms.
o.s.j.d.i.ScriptUtils - Executing SQL script from class path resource [db/migration/V1_0002__queue_server_entries.sql]
o.s.j.d.i.ScriptUtils - Executed SQL script from class path resource [db/migration/V1_0002__queue_server_entries.sql] in 8 ms.
o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default'

I am able to invoke the REST webservice and the call goes through to the Repository but nothing is returned. The application works fine when connecting to mysql and returns data that was loaded. I cannot see what is missing WRT configuration:

Testcase:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {QueueServiceApplication.class, TestConfig.class})
@WebAppConfiguration
@ActiveProfiles({"test"})
public class QueueServiceApplicationTests {

    private static final int EXPECTED_SERVER_COUNT = 10;

    @Autowired
    private WebApplicationContext webCtx;

    private MockMvc mvc;

    @Before
    public void init() {
        mvc = MockMvcBuilders.webAppContextSetup(webCtx).build();
    }

    @Test
    public void successListAll() throws Exception {
    mvc.perform(get("/listall")).andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))
            .andExpect(jsonPath("$", hasSize(EXPECTED_SERVER_COUNT)));


    }
}

Test configuration object for embedded datasource:

@Configuration
@Profile("test")
public class TestConfig {
    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
            .addScript("classpath:/db/migration/V1_0001__create_queue_server_table.sql")
            .addScript("classpath:/db/migration/V1_0002__queue_server_entries.sql")
            .setType(EmbeddedDatabaseType.H2)
            .setName("vitel-balancer")
            .setScriptEncoding("UTF8")
            .build();
    }
}

Launcher:

@SpringBootApplication
public class QueueServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(QueueServiceApplication.class, args);
    }
}

I'm using spring-boot 1.3.2, java 8 update 72

Aucun commentaire:

Enregistrer un commentaire