Skip to content

Instantly share code, notes, and snippets.

@Romeh

Romeh/DAoTest.java

Last active Mar 6, 2019
Embed
What would you like to do?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {DbConfig.class})
@ActiveProfiles("DaoTest")
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:dao/TestData.sql")
public class PostgresEmbeddedDaoTestingApplicationTests {
/* Here we have a static postgreSQL test container Class rule to make the instance used
for all test methods in the same test class , and we use @Transactional to avoid any dirty data changes between
different test methods , where we pass our test database configuration ,
ideally that should be loaded from external config file */
@ClassRule
public static PostgreSQLContainer postgreSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer(
"postgres:10.3")
.withDatabaseName("test")
.withUsername("user")
.withPassword("pass").withStartupTimeout(Duration.ofSeconds(600));
@Autowired
private CustomerRepository customerRepository;
@Test
@Transactional
public void contextLoads() {
customerRepository.save(Customer.builder()
.id(new Random().nextInt())
.address("brussels")
.name("TestName")
.build());
Assert.assertTrue(customerRepository.findCustomerByName("TestName") != null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.