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) | |
@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 |
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
@Bean | |
public SpringLiquibase springLiquibase(DataSource dataSource) throws SQLException { | |
// here we create the schema first if not yet created before | |
tryToCreateSchema(dataSource); | |
SpringLiquibase liquibase = new SpringLiquibase(); | |
// we want to drop the datasbe if it was created before to have immutable version | |
liquibase.setDropFirst(true); | |
liquibase.setDataSource(dataSource); | |
//you set the schema name which will be used into ur integration test | |
liquibase.setDefaultSchema("test"); |
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
@Bean | |
public DataSource dataSource() { | |
DriverManagerDataSource ds = new DriverManagerDataSource(); | |
ds.setDriverClassName("org.postgresql.Driver"); | |
// 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.getMappedPort( | |
PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName())); | |
ds.setUsername(postgreSQLContainer.getUsername()); | |
ds.setPassword(postgreSQLContainer.getPassword()); |
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
<dependency> | |
<groupId>org.liquibase</groupId> | |
<artifactId>liquibase-maven-plugin</artifactId> | |
<version>3.6.3</version> | |
</dependency> | |
<dependency> | |
<groupId>org.testcontainers</groupId> | |
<artifactId>postgresql</artifactId> | |
<version>1.10.5</version> |
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
// 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")) | |
.maxAttempts(2).build(); | |
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(); |
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
// Given the HelloWorldService returns Hello world | |
given(helloWorldService.returnHelloWorld()) | |
.willReturn(completedFuture("Hello world")); | |
final AsyncRetry retryContext = AsyncRetry.of("retryConfig", | |
// we set the response type to String | |
RetryConfig.<String>custom() | |
// max retry attempts | |
.maxAttempts(3) | |
// what are the ignore exception to no retry on |
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
compile "io.github.resilience4j:resilience4j-core:0.13.2" | |
compile "io.github.resilience4j:resilience4j-retry:0.13.2" |
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
<dependency> | |
<groupId>io.github.resilience4j</groupId> | |
<artifactId>resilience4j-retry</artifactId> | |
<version>0.13.2</version> | |
</dependency> | |
<dependency> | |
<groupId>io.github.resilience4j</groupId> | |
<artifactId>resilience4j-core</artifactId> | |
<version>0.13.2</version> | |
</dependency> |
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
@ExtendWith(SpringExtension.class) | |
@SpringBootTest | |
public class SpringBootJunit5IntegrationTest { | |
@Autowired | |
private ShowService showService; | |
@Test | |
@DisplayName("Integration test which will get the actual output of text service") | |
public void contextLoads() { |
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
@ExtendWith(MockitoExtension.class) | |
@DisplayName("Spring boot 2 mockito2 Junit5 example") | |
public class ShowServiceTests { | |
private static final String MOCK_OUTPUT = "Mocked show label"; | |
@Mock | |
private TextService textService; | |
@InjectMocks | |
private ShowService showService; |
NewerOlder