Skip to content

Instantly share code, notes, and snippets.

View Romeh's full-sized avatar

MRomeh Romeh

View GitHub Profile
@ContextConfiguration(classes = {DbConfig.class})
@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 */
public SpringLiquibase springLiquibase(DataSource dataSource) throws SQLException {
// here we create the schema first if not yet created before
SpringLiquibase liquibase = new SpringLiquibase();
// we want to drop the datasbe if it was created before to have immutable version
//you set the schema name which will be used into ur integration test
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
// here we reference the static test container variable in our test case to get the used the connection details
ds.setUrl(format("jdbc:postgresql://%s:%s/%s", postgreSQLContainer.getContainerIpAddress(),
PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName()));
// Given the HelloWorldService returns Hello world
BDDMockito.given(helloWorldService.returnHelloWorld()).willReturn("Hello world");
// Create a Retry with default configuration
final RetryConfig tryAgain = RetryConfig.<String>custom().retryOnResult(s -> s.contains("Hello world"))
Retry retry = Retry.of("id", tryAgain);
// Decorate the invocation of the HelloWorldService
Supplier<String> supplier = Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld);
// When
String result = supplier.get();
Romeh /
Last active December 4, 2018 16:45
// Given the HelloWorldService returns Hello world
.willReturn(completedFuture("Hello world"));
final AsyncRetry retryContext = AsyncRetry.of("retryConfig",
// we set the response type to String
// max retry attempts
// what are the ignore exception to no retry on
compile "io.github.resilience4j:resilience4j-core:0.13.2"
compile "io.github.resilience4j:resilience4j-retry:0.13.2"
public class SpringBootJunit5IntegrationTest {
private ShowService showService;
@DisplayName("Integration test which will get the actual output of text service")
public void contextLoads() {
@DisplayName("Spring boot 2 mockito2 Junit5 example")
public class ShowServiceTests {
private static final String MOCK_OUTPUT = "Mocked show label";
private TextService textService;
private ShowService showService;