BAEL2941: Using JUnit SpringJUnit4ClassRunner with Parametrized
This commit is contained in:
@@ -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;
|
||||
}
|
||||
+32
@@ -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<String, Role> 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
|
||||
}
|
||||
}
|
||||
+49
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
+73
@@ -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<Object[]> data() {
|
||||
Collection<Object[]> 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));
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -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<Object[]> data() {
|
||||
Collection<Object[]> 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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user