Skip to content

Commit d84daee

Browse files
committed
refactor: migrate unit tests (on Java) to JUnit 5
Notes: - only unit tests already written on Java have been migrated. Tests on Spock will be migrated in #1246 - pmd: JUnit4SuitesShouldUseSuiteAnnotation check has been disabled because it's junit3/4 specific - pmd: JUnitAssertionsShouldIncludeMessage check has been disabled because we use AssertJ assestions Changelogs: - https://junit.org/junit5/docs/5.0.0/user-guide/#release-notes - https://junit.org/junit5/docs/5.1.0/release-notes/index.html - https://junit.org/junit5/docs/5.2.0/release-notes/index.html - https://junit.org/junit5/docs/5.3.2/release-notes/index.html Fix #1160
1 parent f74efd7 commit d84daee

File tree

8 files changed

+44
-22
lines changed

8 files changed

+44
-22
lines changed

Diff for: NEWS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
0.x (upcoming release)
2+
(infrastructure) migrate to JUnit 5
23

34
0.4.5
45
(infrastructure) migrate to Spring Boot 2.1

Diff for: pom.xml

+23-1
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,27 @@
428428
<scope>test</scope>
429429
</dependency>
430430

431+
<dependency>
432+
<groupId>org.junit.jupiter</groupId>
433+
<artifactId>junit-jupiter-api</artifactId>
434+
<version>${junit-jupiter.version}</version>
435+
<scope>test</scope>
436+
</dependency>
437+
438+
<dependency>
439+
<groupId>org.junit.jupiter</groupId>
440+
<artifactId>junit-jupiter-engine</artifactId>
441+
<version>${junit-jupiter.version}</version>
442+
<scope>test</scope>
443+
</dependency>
444+
445+
<dependency>
446+
<groupId>org.junit.vintage</groupId>
447+
<artifactId>junit-vintage-engine</artifactId>
448+
<version>${junit-jupiter.version}</version>
449+
<scope>test</scope>
450+
</dependency>
451+
431452
<dependency>
432453
<groupId>org.seleniumhq.selenium</groupId>
433454
<artifactId>htmlunit-driver</artifactId>
@@ -493,7 +514,7 @@
493514

494515
<dependency>
495516
<groupId>org.togglz</groupId>
496-
<artifactId>togglz-junit</artifactId>
517+
<artifactId>togglz-junit5</artifactId>
497518
<version>${togglz.version}</version>
498519
<scope>test</scope>
499520
</dependency>
@@ -601,6 +622,7 @@
601622
<jsoup.version>1.13.1</jsoup.version>
602623

603624
<!-- Redefine default value from spring-boot-dependencies -->
625+
<junit-jupiter.version>5.3.2</junit-jupiter.version>
604626
<junit.version>4.13.1</junit.version>
605627

606628
<license.plugin.version>3.0</license.plugin.version>

Diff for: src/main/config/pmd.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
<rule ref="category/java/bestpractices.xml/CheckResultSet" />
2121
<rule ref="category/java/bestpractices.xml/ConstantsInInterface" />
2222
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitchStmt" />
23-
<rule ref="category/java/bestpractices.xml/JUnit4SuitesShouldUseSuiteAnnotation" />
23+
<!--<rule ref="category/java/bestpractices.xml/JUnit4SuitesShouldUseSuiteAnnotation" />-->
2424
<rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseAfterAnnotation" />
2525
<rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseBeforeAnnotation" />
2626
<rule ref="category/java/bestpractices.xml/JUnit4TestShouldUseTestAnnotation" />
27-
<rule ref="category/java/bestpractices.xml/JUnitAssertionsShouldIncludeMessage" />
27+
<!--<rule ref="category/java/bestpractices.xml/JUnitAssertionsShouldIncludeMessage" />-->
2828
<rule ref="category/java/bestpractices.xml/JUnitTestContainsTooManyAsserts" />
2929
<rule ref="category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert" />
3030
<rule ref="category/java/bestpractices.xml/JUnitUseExpected" />

Diff for: src/main/java/ru/mystamps/web/support/spring/security/ContentSecurityPolicyHeaderWriter.java

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class ContentSecurityPolicyHeaderWriter implements HeaderWriter {
157157
private final String h2ConsolePath;
158158

159159

160+
// @todo #1160 ContentSecurityPolicyHeaderWriter shouldn't depend from Togglz
160161
@Override
161162
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
162163
String uri = request.getRequestURI();

Diff for: src/test/java/ru/mystamps/web/feature/category/JdbcCategoryDaoTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
package ru.mystamps.web.feature.category;
1919

2020
import org.assertj.core.api.WithAssertions;
21-
import org.junit.Test;
22-
import org.junit.runner.RunWith;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
2525
import org.springframework.test.context.ContextConfiguration;
2626
import org.springframework.test.context.TestPropertySource;
2727
import org.springframework.test.context.jdbc.Sql;
28-
import org.springframework.test.context.junit4.SpringRunner;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
2929
import ru.mystamps.web.config.DaoConfig;
3030
import ru.mystamps.web.tests.Random;
3131

@@ -43,7 +43,7 @@
4343
},
4444
// @todo #1143 Improve a way of importing properties files in the tests
4545
locations = "/sql/category_dao_queries.properties")
46-
@RunWith(SpringRunner.class)
46+
@ExtendWith(SpringExtension.class)
4747
public class JdbcCategoryDaoTest implements WithAssertions {
4848

4949
@Autowired

Diff for: src/test/java/ru/mystamps/web/feature/country/JdbcCountryDaoTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
package ru.mystamps.web.feature.country;
1919

2020
import org.assertj.core.api.WithAssertions;
21-
import org.junit.Test;
22-
import org.junit.runner.RunWith;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
2525
import org.springframework.test.context.ContextConfiguration;
2626
import org.springframework.test.context.TestPropertySource;
2727
import org.springframework.test.context.jdbc.Sql;
28-
import org.springframework.test.context.junit4.SpringRunner;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
2929
import ru.mystamps.web.config.DaoConfig;
3030
import ru.mystamps.web.tests.Random;
3131

@@ -43,7 +43,7 @@
4343
},
4444
// LATER: find a better way for importing properties files (see #1151)
4545
locations = "/sql/country_dao_queries.properties")
46-
@RunWith(SpringRunner.class)
46+
@ExtendWith(SpringExtension.class)
4747
public class JdbcCountryDaoTest implements WithAssertions {
4848

4949
@Autowired

Diff for: src/test/java/ru/mystamps/web/feature/series/importing/extractor/JsoupSiteParserTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import org.jsoup.Jsoup;
2222
import org.jsoup.nodes.Document;
2323
import org.jsoup.nodes.Element;
24-
import org.junit.Before;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
2626
import ru.mystamps.web.tests.Random;
2727
import java.util.Locale;
2828

@@ -34,7 +34,7 @@ public class JsoupSiteParserTest implements WithAssertions {
3434

3535
private JsoupSiteParser parser;
3636

37-
@Before
37+
@BeforeEach
3838
public void init() {
3939
parser = new JsoupSiteParser();
4040
}

Diff for: src/test/java/ru/mystamps/web/support/spring/security/ContentSecurityPolicyHeaderWriterTest.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
package ru.mystamps.web.support.spring.security;
1919

2020
import org.assertj.core.api.WithAssertions;
21-
import org.junit.Rule;
22-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2322
import org.springframework.mock.web.MockHttpServletRequest;
2423
import org.springframework.mock.web.MockHttpServletResponse;
25-
import org.togglz.junit.TogglzRule;
24+
import org.togglz.junit5.AllEnabled;
25+
import org.togglz.testing.TestFeatureManager;
2626
import ru.mystamps.web.feature.site.SiteUrl;
2727
import ru.mystamps.web.support.togglz.Features;
2828
import ru.mystamps.web.tests.Random;
@@ -41,15 +41,13 @@ public class ContentSecurityPolicyHeaderWriterTest implements WithAssertions {
4141
private static final int NUMBER_OF_DIRECTIVES_ON_H2_CONSOLE_PAGE = 7;
4242
private static final String H2_CONSOLE_PATH = "/console/";
4343

44-
@Rule
45-
public TogglzRule togglz = TogglzRule.allEnabled(Features.class);
46-
4744
//
4845
// Tests for writeHeaders()
4946
//
5047

5148
@Test
52-
public void writeContentSecurityPolicyHeader() {
49+
@AllEnabled(Features.class)
50+
public void writeContentSecurityPolicyHeader(TestFeatureManager featureManager) {
5351
// given
5452
ContentSecurityPolicyHeaderWriter writer = new ContentSecurityPolicyHeaderWriter(
5553
bool(),
@@ -68,7 +66,7 @@ public void writeContentSecurityPolicyHeader() {
6866
assertThat(header.split(";")).hasSize(NUMBER_OF_DIRECTIVES_ON_STANDARD_PAGES);
6967

7068
// when
71-
togglz.disable(Features.CSP_REPORT_ONLY);
69+
featureManager.disable(Features.CSP_REPORT_ONLY);
7270
writer.writeHeaders(request, response);
7371
// then
7472
header = response.getHeader("Content-Security-Policy");

0 commit comments

Comments
 (0)