Skip to content

Instantly share code, notes, and snippets.

Billy Korando wkorando

  • IBM
Block or report user

Report or block wkorando

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View ITStormRepoAlternate.java
@Testcontainers
@SpringJUnitConfig
@ContextConfiguration(classes = {
StormTrackerApplication.class }, initializers = ITStormRepoAlternate.Initializer.class)
@TestPropertySource("classpath:application.properties")
@TestMethodOrder(OrderAnnotation.class)
public class ITStormRepoAlternate {
@Container
private static PostgreSQLContainer container = new PostgreSQLContainer("postgres:11.2");//Can be an arbitrary image name and tag
View ITStormRepoAlternate-full.java
package com.ibm.developer.stormtracker;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.util.TestPropertyValues;
View testcontainers-postgres-dependency.xml
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.11.1</version>
<scope>test</scope>
</dependency>
View ITStormRepo-full.java
package com.ibm.developer.stormtracker;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.util.TestPropertyValues;
View ITStormRepo.java
@SpringJUnitConfig
@ContextConfiguration(classes = { StormTrackerApplication.class }, initializers = ITStormRepo.Initializer.class)
@TestPropertySource("classpath:application.properties")
@TestMethodOrder(OrderAnnotation.class)
public class ITStormRepo {
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of("spring.datasource.url=jdbc:tc:postgresql:11.2://arbitrary/arbitrary", //
View TestUserService.java
@ExtendWith(MockitoExtension.class)
public class TestUserService {
@Nested
class TestWithMethodInjection {
@Test
public void testAddNewUserAndAddress(@Mock UserDao userDao, @Mock AddressDao addressDao) {
when(userDao.addNewUser(any())).thenCallRealMethod();
when(addressDao.addNewAddress(any())).thenCallRealMethod();
UserService service = new UserService(userDao, addressDao);
View TestHandleExceptionsJUnit5.java
public class TestHandleExceptionsJUnit5 {
@Test
public void testExceptionHandling() {
Exception e = assertThrows(SpecificException.class, () -> onlyThrowsExceptions());
assertEquals("An exception was thrown!", e.getMessage());
}
@Test
public void testExceptionHandlingFailWrongExceptionType() {
View TestHandleExceptionsJUnit4.java
public class TestHandleExceptionsJUnit4 {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test(expected = SpecificException.class)
public void testSpecificExceptionHandling() throws SpecificException {
onlyThrowsExceptions();
}
View TestKit-pom.xml
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<scope>test</scope>
</dependency>
View ExtensionOrdering.java
public class TestExtensionOrdering {
@RegisterExtension
@Order(3)
static ExampleJUnit5Extension extensionA = new ExampleJUnit5Extension("A");
@RegisterExtension
@Order(2)
static ExampleJUnit5Extension extensionB = new ExampleJUnit5Extension("B");
@RegisterExtension
@Order(1)
static ExampleJUnit5Extension extensionC = new ExampleJUnit5Extension("C");
You can’t perform that action at this time.