From 567fe3151ce7c232314dc7b8a8dcabffdf08ad2e Mon Sep 17 00:00:00 2001 From: Chirag Dewan Date: Sat, 18 May 2019 19:20:02 +0530 Subject: [PATCH] BAEL2941: Using JUnit SpringJUnit4ClassRunner with Parametrized --- testing-modules/spring-testing/pom.xml | 12 +++ .../java/com/baeldung/config/WebConfig.java | 17 +++++ .../parameterized/EmployeeRoleController.java | 32 ++++++++ .../RoleControllerIntegrationTest.java | 49 +++++++++++++ ...ParameterizedClassRuleIntegrationTest.java | 73 +++++++++++++++++++ ...ontrollerParameterizedIntegrationTest.java | 69 ++++++++++++++++++ 6 files changed, 252 insertions(+) create mode 100644 testing-modules/spring-testing/src/main/java/com/baeldung/config/WebConfig.java create mode 100644 testing-modules/spring-testing/src/main/java/com/baeldung/controller/parameterized/EmployeeRoleController.java create mode 100644 testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerIntegrationTest.java create mode 100644 testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedClassRuleIntegrationTest.java create mode 100644 testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedIntegrationTest.java diff --git a/testing-modules/spring-testing/pom.xml b/testing-modules/spring-testing/pom.xml index 630aed0c81..10d34f169b 100644 --- a/testing-modules/spring-testing/pom.xml +++ b/testing-modules/spring-testing/pom.xml @@ -44,6 +44,11 @@ spring-context LATEST + + org.springframework + spring-webmvc + ${spring.version} + org.eclipse.persistence javax.persistence @@ -66,6 +71,11 @@ ${awaitility.version} test + + javax.servlet + javax.servlet-api + ${servlet.api.version} + @@ -84,6 +94,8 @@ 2.0.0.0 3.1.6 5.4.0 + 5.1.4.RELEASE + 4.0.1 \ No newline at end of file diff --git a/testing-modules/spring-testing/src/main/java/com/baeldung/config/WebConfig.java b/testing-modules/spring-testing/src/main/java/com/baeldung/config/WebConfig.java new file mode 100644 index 0000000000..eca0aed57f --- /dev/null +++ b/testing-modules/spring-testing/src/main/java/com/baeldung/config/WebConfig.java @@ -0,0 +1,17 @@ +package com.baeldung.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +import javax.servlet.ServletContext; + +@EnableWebMvc +@Configuration +@ComponentScan(basePackages = {"com.baeldung.controller.parameterized"}) +public class WebConfig { + + @Autowired + private ServletContext ctx; +} diff --git a/testing-modules/spring-testing/src/main/java/com/baeldung/controller/parameterized/EmployeeRoleController.java b/testing-modules/spring-testing/src/main/java/com/baeldung/controller/parameterized/EmployeeRoleController.java new file mode 100644 index 0000000000..0f1ed9e61d --- /dev/null +++ b/testing-modules/spring-testing/src/main/java/com/baeldung/controller/parameterized/EmployeeRoleController.java @@ -0,0 +1,32 @@ +package com.baeldung.controller.parameterized; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.HashMap; +import java.util.Map; + +@Controller +public class EmployeeRoleController { + + private static Map userRoleCache = new HashMap<>(); + + static { + userRoleCache.put("John", Role.ADMIN); + userRoleCache.put("Doe", Role.EMPLOYEE); + } + + @RequestMapping(value = "/role/{name}", method = RequestMethod.GET, produces = "application/text") + @ResponseBody + public String getEmployeeRole(@PathVariable("name") String employeeName) { + + return userRoleCache.get(employeeName).toString(); + } + + private enum Role { + ADMIN, EMPLOYEE + } +} diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerIntegrationTest.java new file mode 100644 index 0000000000..c362067cc0 --- /dev/null +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerIntegrationTest.java @@ -0,0 +1,49 @@ +package com.baeldung.controller.parameterized; + +import com.baeldung.config.WebConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebConfig.class) +public class RoleControllerIntegrationTest { + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1"; + + @Before + public void setup() throws Exception { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } + + @Test + public void givenEmployeeNameJohnWhenInvokeRoleThenReturnAdmin() throws Exception { + this.mockMvc.perform(MockMvcRequestBuilders.get("/role/John")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)) + .andExpect(MockMvcResultMatchers.content().string("ADMIN")); + } + + @Test + public void givenEmployeeNameDoeWhenInvokeRoleThenReturnEmployee() throws Exception { + this.mockMvc.perform(MockMvcRequestBuilders.get("/role/Doe")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)) + .andExpect(MockMvcResultMatchers.content().string("EMPLOYEE")); + } + +} \ No newline at end of file diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedClassRuleIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedClassRuleIntegrationTest.java new file mode 100644 index 0000000000..eca294a742 --- /dev/null +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedClassRuleIntegrationTest.java @@ -0,0 +1,73 @@ +package com.baeldung.controller.parameterized; + +import com.baeldung.config.WebConfig; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestContextManager; +import org.springframework.test.context.junit4.rules.SpringClassRule; +import org.springframework.test.context.junit4.rules.SpringMethodRule; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.util.ArrayList; +import java.util.Collection; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +@RunWith(Parameterized.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebConfig.class) +public class RoleControllerParameterizedClassRuleIntegrationTest { + + private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1"; + + @ClassRule + public static final SpringClassRule scr = new SpringClassRule(); + + @Rule + public final SpringMethodRule smr = new SpringMethodRule(); + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + @Parameter(value = 0) + public String name; + + @Parameter(value = 1) + public String role; + + @Parameters + public static Collection data() { + Collection params = new ArrayList(); + params.add(new Object[]{"John", "ADMIN"}); + params.add(new Object[]{"Doe", "EMPLOYEE"}); + + return params; + } + + @Before + public void setup() throws Exception { + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } + + @Test + public void givenEmployeeNameWhenInvokeRoleThenReturnRole() throws Exception { + this.mockMvc.perform(MockMvcRequestBuilders.get("/role/" + name)).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)) + .andExpect(MockMvcResultMatchers.content().string(role)); + } + +} \ No newline at end of file diff --git a/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedIntegrationTest.java b/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedIntegrationTest.java new file mode 100644 index 0000000000..1458b24b06 --- /dev/null +++ b/testing-modules/spring-testing/src/test/java/com/baeldung/controller/parameterized/RoleControllerParameterizedIntegrationTest.java @@ -0,0 +1,69 @@ +package com.baeldung.controller.parameterized; + +import com.baeldung.config.WebConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestContextManager; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.util.ArrayList; +import java.util.Collection; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; + +@RunWith(Parameterized.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebConfig.class) +public class RoleControllerParameterizedIntegrationTest { + + private static final String CONTENT_TYPE = "application/text;charset=ISO-8859-1"; + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + private TestContextManager testContextManager; + + @Parameter(value = 0) + public String name; + + @Parameter(value = 1) + public String role; + + @Parameters + public static Collection data() { + Collection params = new ArrayList(); + params.add(new Object[]{"John", "ADMIN"}); + params.add(new Object[]{"Doe", "EMPLOYEE"}); + + return params; + } + + @Before + public void setup() throws Exception { + this.testContextManager = new TestContextManager(getClass()); + this.testContextManager.prepareTestInstance(this); + + this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); + } + + @Test + public void givenEmployeeNameWhenInvokeRoleThenReturnRole() throws Exception { + this.mockMvc.perform(MockMvcRequestBuilders.get("/role/" + name)).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(CONTENT_TYPE)) + .andExpect(MockMvcResultMatchers.content().string(role)); + } + +} \ No newline at end of file