0

I have a SpringBootApplicationWhich I wish to test.

Below are the details about my files

application.properties

PRODUCT_DATABASE_PASSWORD=
PRODUCT_DATABASE_USERNAME=sa
PRODUCT_DATABASE_CONNECTION_URL=jdbc:h2:file:./target/db/testdb
PRODUCT_DATABASE_DRIVER=org.h2.Driver
RED_SHIFT_DATABASE_PASSWORD=
RED_SHIFT_DATABASE_USERNAME=sa
RED_SHIFT_DATABASE_CONNECTION_URL=jdbc:h2:file:./target/db/testdb
RED_SHIFT_DATABASE_DRIVER=org.h2.Driver
spring.datasource.platform=h2

ConfigurationClass

@SpringBootConfiguration
@SpringBootApplication
@Import({ProductDataAccessConfig.class, RedShiftDataAccessConfig.class})
public class TestConfig {
}

Main Test Class

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {TestConfig.class,ConfigFileApplicationContextInitializer.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE)
 public class MainTest {
 @Autowired(required = true)
 @Qualifier("dataSourceRedShift")
 private DataSource dataSource;
 @Test
 public void testHourlyBlock() throws Exception {
 insertDataIntoDb(); //data sucessfully inserted
 SpringApplication.run(Application.class, new String[]{}); //No data found
 }
}

Data Access In Application.class;

 try (Connection conn = dataSourceRedShift.getConnection();
 Statement stmt = conn.createStatement() {
//access inserted data
}

Please Help! PS for the spring boot application the test beans are being picked so bean instantiation definitely not a problem. I think I am missing some properties.

I do not use hibernate in my application and data goes off even within the same application context (child context). i.e. I run a spring boot application which reads that data inserted earlier

asked Sep 21, 2016 at 22:07
5
  • In the test, by default the changes will rollback, do you disable the transaction rollback? Commented Sep 22, 2016 at 0:40
  • I am not using any transaction manager. Is it configured by default? I did try connection.commit after each query didn't help! Commented Sep 22, 2016 at 0:46
  • You can refer to thie docs Commented Sep 22, 2016 at 0:53
  • I do not use hibernate in my application and data goes off even within the same application context (child context). i.e. I run a spring boot application which reads that data inserted earlier. Should I still implement transaction manager? Commented Sep 22, 2016 at 0:58
  • So you need add all those info in your question. Commented Sep 22, 2016 at 1:01

1 Answer 1

2

Problem solved. removing spring.datasource.platform=h2 from the application.properties. Made my h2 data persists. But I still wish to know how is h2 starting automatically?

answered Sep 26, 2016 at 20:27
Sign up to request clarification or add additional context in comments.

2 Comments

probably because of autoconfigurations h2 is enabled. By adding a h2 dependency springboot will auto configure h2 db.
Thanks, I was having the same problem, and now it's working.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.