You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 4
Next »
Motivation
New useful functionality will be added:
Check to throw exceptions:
public void shouldRaiseAnException () throws Exception {
Assertions.assertThrows(Exception.class, () -> {
//...
});
}
In JUnit4:
@Test(expected = Exception.class)
Ability to check timeouts:
@Test(timeout = 1)
public void shouldFailBecauseTimeout () throws InterruptedException {
Thread.sleep(10);
}
@Test
public void shouldFailBecauseTimeout () throws InterruptedException {
Assertions.assertTimeout(Duration.ofMillis(1), () -> Thread.sleep(10));
}
Compatibility with Java8, it is possible to write lambdas directly from Assert:
@Test
public void shouldFailBecauseTheNumbersAreNotEqual_lazyEvaluation () {
Assertions.assertTrue(
2 == 3,
() -> "Numbers " + 2 + " and " + 3 + " are not equal!");
}
@Test
public void shouldAssertAllTheGroup () {
List<Integer> list = Arrays.asList(1, 2, 4);
Assertions.assertAll("List is not incremental",
() -> Assertions.assertEquals(list.get(0).intValue(), 1),
() -> Assertions.assertEquals(list.get(1).intValue(), 2),
() -> Assertions.assertEquals(list.get(2).intValue(), 3));
}
Assumption. Tests will be executed under conditions:
@Test
public void whenEnvironmentIsWeb_thenUrlsShouldStartWithHttp () {
assumingThat("WEB".equals(System.getenv("ENV")),
() -> {
assertTrue("http".startsWith(address));
});
}
Useful to check flaky tests:
@RepeatedTest(value = 3, name = "Custom name {currentRepetition}/{totalRepetitions}")
void repeatedTestWithCustomDisplayName (TestInfo testInfo){
assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
}
@ParameterizedTest
@ValueSource(strings = {"Hello", "JUnit"})
void withValueSource (String word){
assertNotNull(word);
}
Migration from JUnit 4 to 5 is not so difficult, as backward compatibility is available.
Description
The new features of JUnit 5 can improve Apache Ignite development process and make testing more effectively. Moreover, JUnit 5 has totally backward compatibility with 4 version (it is achievable with JUpiter module).
Process Overview
- Migrate examples-module.
- Add dependencies to parent/pom.xml
- Change adding suites to IgniteExamplesSelfTestSuite
- Launch IgniteExamplesMLTestSuite separately under JUnit4, because it is impossible to override JUnitPlatform constructor as well as Suite constructor
- Change annotations and imports in test classes
- Ignore commented tests
- Migrate core module in the same way as examples. Classes with dynamic tests do not need to be changed. This is due to the fact that in JUnit5 it is not possible to launch static and dynamic tests under one surefire version.
Positive side: the dynamic tests amount is not so big, they locate in specific places. The all JUnit5 opportunity can not be used in dynamic tests (@TestFactory).
According to the previous points, the next should be added:- Remove test timeouts and add timeouts via annotations
- Replace exception checking according to JUnit5 style
- Replace some tests on parameterized where it is necessary (the same tests are used with different parameters)
- Migrate modules where JUnit tests exist. Check list:
- aop
- aws
- camel
- cassandra
- clients
- cloud
- comatibility
- compress
- direct-io
- flink
- flume
- gce
- geospastial
- hadoop
- hibernate 4.2, 5.1, 5.3, core
- ignored
- indexing
- jcl
- jta
- kafka
- kubernates
- log4j
- log4j2
- ml
- mqtt
- osgi
- rest-htpp
- rocketmql
- schedule
- slf4j
- spark
- spring, spring-data, 2.0
- ssh
- storm
- tensorflow
- twitter
- urldeploy
- web-console
- yarn
- zeromq
- zookeeper
Completion criteria for each point:
Tests can be launched under JUnit5. Usage of JUnit 5 annotations becomes available, for example, @RepeatedTest
There are no additional fails on TeamCity.
Completion criteria on the whole:
All modules satisfy the previous two criteria
The community is informed about the migration to 5 and benefits of the newest version.
Additional improvements to JUnit 3->4 migration.
Remove JUnit3TestLegacyAssertclass. Replace inheritance to imports
In JUnit3TestLegacySupport:
- Replace tearDown, setUp by @BeforeEach, @AfterEach annotations, where it is necessary
- The same situation with beforeTest, afterTest methods
- Replace beforeTestsStarted, afterTestsStopped by @BeforeAll, @AfterAll
- The rest methods move to GridAbstractTest.
Risks and Assumptions
- Additional fails on TC because of migration issues
- Performance degradation of tests execution.
Discussion Links
Devlist discussion.
Reference Links
Question to JUnit develop team about surefire version.
Tickets
key |
summary |
type |
created |
updated |
due |
assignee |
reporter |
priority |
status |
resolution |
JQL and issue key arguments for this macro require at least one Jira application link to be configured
|