2. JUnit 4 JUnit 5
import org.junit.Test;
public class JUnit4Test {
@Test
public void aJunit4TestCase() {
// write test here
}
}
import org.junit.gen5.api.Test;
class JUnit5Test {
@Test
void aJunit5TestCase() {
// write test here
}
}
3. public @interface Test {
Class<? extends Throwable>
expected() default None.class;
long timeout() default 0L;
}
JUnit 4 JUnit 5
public @interface Test {
}
public @interface Before {}
public @interface After {}
public @interface BeforeEach {}
public @interface AfterEach {}
public @interface BeforeClass {}
public @interface AfterClass {}
public @interface BeforeAll {}
public @interface AfterAll {}
public @interface Ignore {
String value() default "";
}
public @interface Disabled {
String value() default "";
}
6. JUnit 4 JUnit 5
public class ATest {
@Test
public void foo() {}
}
public class BTest {
@Test
public void bar() {}
}
@RunWith(Suite.class)
@Suite.SuiteClasses({
ATest.class,
BTest.class
})
public class MySuite {}
@Tag(SuiteA.class)
public class ATest {
@Test
public void foo() {}
}
@Tags(
@Tag(SuiteA.class),
@Tag(SuiteB.class)
)
public class BTest {
@Test
public void bar() {}
}
7. JUnit 4 JUnit 5
public class ATest {
@Test
public void foo() {}
}
public class BTest {
@Test
public void bar() {}
}
@RunWith(Suite.class)
@Suite.SuiteClasses({
ATest.class,
BTest.class
})
public class MySuite {}
@Tag(SuiteA.class)
public class ATest {
@Test
public void foo() {}
}
@Tag(SuiteA.class)
@Tag(SuiteB.class)
@Retention(RUNTIME)
public @interface Meta {}
@Meta
public class BTest {
@Test
public void bar() {}
}
8. @DisplayName("A stack")
class TestingAStack {
Stack<Object> stack = new Stack<Object>();
@Test @DisplayName("is empty") void isEmpty() {
Assertions.assertTrue(stack.isEmpty());
}
@Nested @DisplayName("after pushing an element")
class AfterPushing {
String anElement = "an element";
@BeforeEach void init() {
stack.push(anElement);
}
@Test @DisplayName("it is no longer empty") void isEmpty() {
Assertions.assertFalse(stack.isEmpty());
}
@Test @DisplayName("returns the element when popped")
void returnElementWhenPopped() {
Assertions.assertEquals(anElement, stack.pop());
}
}
}
9. class DependencyInjection {
@Test
void testSelfAware(TestInfo testInfo) {
String name = testInfo.getName();
assertTrue("selfAwareTest", name);
}
}
public interface TestInfo {
String getName();
String getDisplayName();
Set<String> getTags();
}
12. @ExtendWith(FooExtension.class)
class FunctionalAssertionsWithHamcrest {
@Test
void testProcedural(@Foo File file) {
assertTrue(file.exists());
}
}
@Retention(RUNTIME)
@interface Foo {}
class FooExtension implements MethodParameterResolver {
@Override public boolean supports(Parameter parameter,
MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext) {
return parameter.isAnnotationPresent(Foo.class);
}
@Override public Object resolve(Parameter parameter,
MethodInvocationContext methodInvocationContext,
ExtensionContext extensionContext){
return new File("some/file");
}
}
13. Currently still unsupported:
1. Parameterized tests (no test-runner equivalent)
2. No test-timeout
3. No sorting of test methods
JInit 5 is work-in-progress and the development has slowed
down significantly. Tickets for M1 are up-for-grab on GitHub!