From ce39a53448ee0e90d0e6c28209ee65e659c24e8c Mon Sep 17 00:00:00 2001 From: divya_jnu08 Date: Thu, 29 Apr 2021 12:44:04 +0530 Subject: [PATCH] Migrate REST examples to JUnit 5. Original pull request: #620. See #583. --- .../rest/headers/ApplicationIntegrationTests.java | 8 +++----- .../rest/headers/CrossOriginIntegrationTests.java | 9 +++------ .../rest/headers/WebIntegrationTests.java | 9 +++------ .../multistore/ApplicationIntegrationTests.java | 5 +---- .../projections/ApplicationIntegrationTests.java | 7 ++----- .../rest/projections/SimpleProjectionTests.java | 4 ++-- .../rest/security/MethodLevelSecurityTests.java | 11 ++++------- .../rest/security/UrlLevelSecurityTests.java | 11 ++++------- .../springdata/rest/stores/StarbucksClient.java | 5 +---- .../stores/StoreRepositoryIntegrationTests.java | 15 ++++++--------- .../springdata/rest/uris/WebIntegrationTests.java | 9 +++------ 11 files changed, 32 insertions(+), 61 deletions(-) diff --git a/rest/headers/src/test/java/example/springdata/rest/headers/ApplicationIntegrationTests.java b/rest/headers/src/test/java/example/springdata/rest/headers/ApplicationIntegrationTests.java index 394ecd73..e9f6932f 100644 --- a/rest/headers/src/test/java/example/springdata/rest/headers/ApplicationIntegrationTests.java +++ b/rest/headers/src/test/java/example/springdata/rest/headers/ApplicationIntegrationTests.java @@ -16,13 +16,12 @@ package example.springdata.rest.headers; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; + /** * Integration tests to bootstrap the application. @@ -30,7 +29,6 @@ import org.springframework.test.context.junit4.SpringRunner; * @author Oliver Gierke * @soundtrack The Intersphere - Out of phase (Live at Alte Feuerwache Mannheim) */ -@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationIntegrationTests { diff --git a/rest/headers/src/test/java/example/springdata/rest/headers/CrossOriginIntegrationTests.java b/rest/headers/src/test/java/example/springdata/rest/headers/CrossOriginIntegrationTests.java index d85ec914..44c6f666 100644 --- a/rest/headers/src/test/java/example/springdata/rest/headers/CrossOriginIntegrationTests.java +++ b/rest/headers/src/test/java/example/springdata/rest/headers/CrossOriginIntegrationTests.java @@ -22,12 +22,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.net.URI; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @@ -37,7 +35,6 @@ import org.springframework.web.context.WebApplicationContext; * * @author Mark Paluch */ -@RunWith(SpringRunner.class) @SpringBootTest public class CrossOriginIntegrationTests { @@ -46,7 +43,7 @@ public class CrossOriginIntegrationTests { MockMvc mvc; - @Before + @BeforeEach public void setUp() { this.mvc = MockMvcBuilders.webAppContextSetup(context).build(); } diff --git a/rest/headers/src/test/java/example/springdata/rest/headers/WebIntegrationTests.java b/rest/headers/src/test/java/example/springdata/rest/headers/WebIntegrationTests.java index 0751c955..091f84df 100644 --- a/rest/headers/src/test/java/example/springdata/rest/headers/WebIntegrationTests.java +++ b/rest/headers/src/test/java/example/springdata/rest/headers/WebIntegrationTests.java @@ -23,14 +23,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.net.URI; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.restdocs.config.RestDocumentationConfigurer; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @@ -40,7 +38,6 @@ import org.springframework.web.util.UriTemplate; * @author Oliver Gierke * @soundtrack The Intersphere - Out of phase (Live at Alte Feuerwache Mannheim) */ -@RunWith(SpringRunner.class) @SpringBootTest public class WebIntegrationTests { @@ -49,7 +46,7 @@ public class WebIntegrationTests { MockMvc mvc; - @Before + @BeforeEach public void setUp() { this.mvc = MockMvcBuilders.webAppContextSetup(context).// diff --git a/rest/multi-store/src/test/java/example/springdata/multistore/ApplicationIntegrationTests.java b/rest/multi-store/src/test/java/example/springdata/multistore/ApplicationIntegrationTests.java index f4448dd2..b4fd5fc2 100644 --- a/rest/multi-store/src/test/java/example/springdata/multistore/ApplicationIntegrationTests.java +++ b/rest/multi-store/src/test/java/example/springdata/multistore/ApplicationIntegrationTests.java @@ -21,11 +21,9 @@ import example.springdata.multistore.treasure.Treasure; import example.springdata.multistore.treasure.TreasureRepository; import lombok.extern.slf4j.Slf4j; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; /** * Integration test to show the usage of repositories backed by different stores. @@ -33,7 +31,6 @@ import org.springframework.test.context.junit4.SpringRunner; * @author Oliver Gierke */ @Slf4j -@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationIntegrationTests { diff --git a/rest/projections/src/test/java/example/springdata/rest/projections/ApplicationIntegrationTests.java b/rest/projections/src/test/java/example/springdata/rest/projections/ApplicationIntegrationTests.java index 6ebe6e79..d9725870 100644 --- a/rest/projections/src/test/java/example/springdata/rest/projections/ApplicationIntegrationTests.java +++ b/rest/projections/src/test/java/example/springdata/rest/projections/ApplicationIntegrationTests.java @@ -16,20 +16,17 @@ package example.springdata.rest.projections; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; /** * Integration tests to bootstrap the application. * * @author Oliver Gierke */ -@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationIntegrationTests { diff --git a/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java b/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java index e0d8fee0..cb2fab57 100644 --- a/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java +++ b/rest/projections/src/test/java/example/springdata/rest/projections/SimpleProjectionTests.java @@ -16,12 +16,12 @@ package example.springdata.rest.projections; import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; import java.util.HashMap; import java.util.Map; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; diff --git a/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java b/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java index 6446eae5..1b14c287 100644 --- a/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java +++ b/rest/security/src/test/java/example/springdata/rest/security/MethodLevelSecurityTests.java @@ -15,17 +15,15 @@ */ package example.springdata.rest.security; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.fail; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.context.junit4.SpringRunner; /** * Collection of test cases used to verify method-level security. @@ -33,13 +31,12 @@ import org.springframework.test.context.junit4.SpringRunner; * @author Greg Turnquist * @author Oliver Gierke */ -@RunWith(SpringRunner.class) @SpringBootTest public class MethodLevelSecurityTests { @Autowired ItemRepository itemRepository; - @Before + @BeforeEach public void setUp() { SecurityContextHolder.clearContext(); } diff --git a/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java b/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java index 742d85f0..024a967c 100644 --- a/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java +++ b/rest/security/src/test/java/example/springdata/rest/security/UrlLevelSecurityTests.java @@ -16,16 +16,15 @@ package example.springdata.rest.security; import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; import java.util.Base64; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.hateoas.MediaTypes; @@ -33,7 +32,6 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.web.FilterChainProxy; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; @@ -45,7 +43,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; * @author Greg Turnquist * @author Oliver Gierke */ -@RunWith(SpringRunner.class) @SpringBootTest public class UrlLevelSecurityTests { @@ -56,7 +53,7 @@ public class UrlLevelSecurityTests { MockMvc mvc; - @Before + @BeforeEach public void setUp() { this.mvc = webAppContextSetup(context).addFilters(filterChain).build(); diff --git a/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java b/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java index 1eac86d9..1815011e 100644 --- a/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java +++ b/rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java @@ -24,8 +24,7 @@ import java.util.HashMap; import java.util.Map; import java.util.stream.StreamSupport; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; @@ -45,7 +44,6 @@ import org.springframework.hateoas.server.core.TypeReferences.CollectionModelTyp import org.springframework.hateoas.server.core.TypeReferences.EntityModelType; import org.springframework.hateoas.server.core.TypeReferences.PagedModelType; import org.springframework.http.RequestEntity; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.client.RestOperations; import org.springframework.web.client.RestTemplate; @@ -54,7 +52,6 @@ import org.springframework.web.client.RestTemplate; * * @author Oliver Gierke */ -@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @Slf4j public class StarbucksClient { diff --git a/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java b/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java index be0aa4a3..ee7b5b96 100644 --- a/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java +++ b/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java @@ -15,17 +15,16 @@ */ package example.springdata.rest.stores; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.*; import example.springdata.rest.stores.Store.Address; import java.util.UUID; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; @@ -33,7 +32,6 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.geo.Distance; import org.springframework.data.geo.Metrics; import org.springframework.data.geo.Point; -import org.springframework.test.context.junit4.SpringRunner; /** * Integration tests for {@link StoreRepository}. @@ -41,14 +39,13 @@ import org.springframework.test.context.junit4.SpringRunner; * @author Oliver Gierke * @author Mark Paluch */ -@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class StoreRepositoryIntegrationTests { @Autowired StoreRepository repository; - @Before - @After + @BeforeEach + @AfterEach public void clearDb() { repository.deleteAll(); } diff --git a/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java b/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java index a276aee5..7778cb50 100644 --- a/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java +++ b/rest/uri-customization/src/test/java/example/springdata/rest/uris/WebIntegrationTests.java @@ -19,12 +19,10 @@ import static org.hamcrest.CoreMatchers.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @@ -35,7 +33,6 @@ import org.springframework.web.context.WebApplicationContext; * @author Oliver Gierke * @soundtrack Clueso - Gewinner (Stadtrandlichter Live) */ -@RunWith(SpringRunner.class) @SpringBootTest public class WebIntegrationTests { @@ -43,7 +40,7 @@ public class WebIntegrationTests { MockMvc mvc; - @Before + @BeforeEach public void setUp() { this.mvc = MockMvcBuilders.webAppContextSetup(context).build(); }