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 TimeoutTest.java
@Timeout(3) // Sets timeout limit for each test in class to five seconds
@TestMethodOrder(OrderAnnotation.class)
public class TimeoutTest {
static int testCounter = 0;
@BeforeAll
@Timeout(2) // If this timeout is exceeded, all tests are failed
public static void classSetupWithTimeout() throws InterruptedException {
// ...complex setup code
View TimeoutTest.java
@Timeout(3) // Sets timeout limit for each test in class to three seconds
@TestMethodOrder(OrderAnnotation.class)
public class TimeoutTest {
static int testCounter = 0;
@BeforeAll
@Timeout(2) // If this timeout is exceeded, all tests are failed
public static void classSetupWithTimeout() throws InterruptedException {
// ...complex setup code
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();
}
You can’t perform that action at this time.