BAEL2941: Using JUnit SpringJUnit4ClassRunner with Parametrized
This commit is contained in:
parent
7a7b07fdd2
commit
567fe3151c
|
@ -44,6 +44,11 @@
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>LATEST</version>
|
<version>LATEST</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.persistence</groupId>
|
<groupId>org.eclipse.persistence</groupId>
|
||||||
<artifactId>javax.persistence</artifactId>
|
<artifactId>javax.persistence</artifactId>
|
||||||
|
@ -66,6 +71,11 @@
|
||||||
<version>${awaitility.version}</version>
|
<version>${awaitility.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
<version>${servlet.api.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -84,6 +94,8 @@
|
||||||
<hamcrest.version>2.0.0.0</hamcrest.version>
|
<hamcrest.version>2.0.0.0</hamcrest.version>
|
||||||
<awaitility.version>3.1.6</awaitility.version>
|
<awaitility.version>3.1.6</awaitility.version>
|
||||||
<junit.jupiter.version>5.4.0</junit.jupiter.version>
|
<junit.jupiter.version>5.4.0</junit.jupiter.version>
|
||||||
|
<spring.version>5.1.4.RELEASE</spring.version>
|
||||||
|
<servlet.api.version>4.0.1</servlet.api.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue