Last active
October 8, 2018 14:12
-
-
Save nate-getch/e1b13ecec91238aee5ef5d6170671142 to your computer and use it in GitHub Desktop.
Junit with H2 DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Further ref https://www.logicbig.com/tutorials/spring-framework/spring-data-access-with-jdbc/jdbc-tests.html