Skip to content

Instantly share code, notes, and snippets.

@TestMethodOrder(OrderAnnotation.class)
public class TestTempDir {
@TempDir
static Path classTempDir;
@TempDir
static File classTempDirAsFile;
@Test
public class TestKitExample {
@Test
void failIfTestsAreSkipped() {
Events testEvents = EngineTestKit
.engine("junit-jupiter")
.selectors(selectClass(TestKitSubject.class))
.execute()
.tests();
public class TestKitSubject {
@Test
public void fakeRunningTest() {
}
@Test
@Disabled
public void fakeDisabledTest() {
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");
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-testkit</artifactId>
<scope>test</scope>
</dependency>
public class TestHandleExceptionsJUnit4 {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test(expected = SpecificException.class)
public void testSpecificExceptionHandling() throws SpecificException {
onlyThrowsExceptions();
}
public class TestHandleExceptionsJUnit5 {
@Test
public void testExceptionHandling() {
Exception e = assertThrows(SpecificException.class, () -> onlyThrowsExceptions());
assertEquals("An exception was thrown!", e.getMessage());
}
@Test
public void testExceptionHandlingFailWrongExceptionType() {
@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);
@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", //
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;