Skip to content

Instantly share code, notes, and snippets.

Avatar

MRomeh Romeh

View GitHub Profile
View DAoTest.java
@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
View SpringLiquibase.java
@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");
View datasourceConfig.java
@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());
View dependencies.xml
<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>
View SyncRetry.java
// 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();
View retry.java
// 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
View resi4j.gradel
compile "io.github.resilience4j:resilience4j-core:0.13.2"
compile "io.github.resilience4j:resilience4j-retry:0.13.2"
View mavenResilence4j.xml
<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>
View SpringBootJunit5IntegrationTest.java
@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() {
View Junit5Test.java
@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;