Skip to content

Instantly share code, notes, and snippets.

@nate-getch
Last active October 8, 2018 14:12
Show Gist options
  • Save nate-getch/e1b13ecec91238aee5ef5d6170671142 to your computer and use it in GitHub Desktop.
Save nate-getch/e1b13ecec91238aee5ef5d6170671142 to your computer and use it in GitHub Desktop.
Junit with H2 DB
@Configuration
@ComponentScan(basePackages={"com.dummy.data"})
public class TestDBConfig {
@Bean
public JdbcTemplate jdbcTemplate()
{
return new JdbcTemplate(h2DataSource());
}
@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate(){
return new NamedParameterJdbcTemplate(h2DataSource());
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public DataSource h2DataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.H2)
.build();
return db;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestConfig.class, properties = { "spring.cloud.config.enabled=false",
"spring.profiles.active=test", "logging.level.org.springframework.jdbc.core.JdbcTemplate=debug" })
@SqlGroup({ @Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts = {
"classpath:sql-file-in-resource-dir.sql" }) })
public class TestDummy {
@Autowired
JdbcTemplate jdbcTemplate;
// autowire DAO object
@Test
public void testMethod() throws Exception {
// mock autowired DAO object
//Mockito.when(daoObject.methodName())
//.thenReturn(someData);
//jdbcTemplate.execute("SQL statment here");
//assertNotNull(list);
//assertEquals(2, list.size());
}
}
@nate-getch
Copy link
Author

nate-getch commented Oct 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment