siye@r480:~/svlution/workspace/springmvc4322$ tree src/
src/
├── main
│ ├── java
│ │ ├── log4j.properties
│ │ └── ocn
│ │ └── site
│ │ └── springmvc
│ │ └── controller
│ │ └── Manicontroller.java
│ ├── resources
│ └── webapp
│ └── WEB-INF
│ └── web.xml
└── test
├── java
│ └── ocn
│ └── site
│ └── springmvc
│ └── controller
│ └── Runtest.java
└── resources
└── config
└── application.xml
17 directories, 5 files
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.22.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.22.RELEASE</version>
<scope>test</scope>
</dependency>
package ocn.site.springmvc.controller;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
@Controller
public class Manicontroller {
private final Logger logger = Logger.getLogger(this.getClass());
@GetMapping("/test")
public void handler(@RequestAttribute String attr) {
logger.info(attr);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="ocn.site.springmvc.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
package ocn.site.springmvc.controller;
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.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
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;
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {
private @Autowired WebApplicationContext wac;
@Test
public void run() throws Exception {
MockMvc build = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/test");
requestBuilder.requestAttr("attr", "hack");
ResultActions ra = build.perform(requestBuilder);
ra.andExpect(MockMvcResultMatchers.status().isOk());
}
}
19-09-05 14:47:43 org.springframework.test.context.web.WebTestContextBootstrapper =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-05 14:47:43 org.springframework.test.context.web.WebTestContextBootstrapper =====>>> Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@6d78f375, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@50c87b21, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5f375618, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1810399e]
19-09-05 14:47:43 org.springframework.beans.factory.xml.XmlBeanDefinitionReader =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-05 14:47:43 org.springframework.web.context.support.GenericWebApplicationContext =====>>> Refreshing org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:47:43 CST 2019]; root of context hierarchy
19-09-05 14:47:44 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping =====>>> Mapped "{[/test],methods=[GET]}" onto public void ocn.site.springmvc.controller.Manicontroller.handler(java.lang.String)
19-09-05 14:47:44 org.hibernate.validator.internal.util.Version =====>>> HV000001: Hibernate Validator 5.2.0.Final
19-09-05 14:47:44 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:47:43 CST 2019]; root of context hierarchy
19-09-05 14:47:44 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter =====>>> Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:47:43 CST 2019]; root of context hierarchy
19-09-05 14:47:44 org.springframework.mock.web.MockServletContext =====>>> Initializing Spring FrameworkServlet ''
19-09-05 14:47:44 org.springframework.test.web.servlet.TestDispatcherServlet =====>>> FrameworkServlet '': initialization started
19-09-05 14:47:44 org.springframework.test.web.servlet.TestDispatcherServlet =====>>> FrameworkServlet '': initialization completed in 19 ms
19-09-05 14:47:44 ocn.site.springmvc.controller.Manicontroller =====>>> hack
19-09-05 14:47:44 org.springframework.web.context.support.GenericWebApplicationContext =====>>> Closing org.springframework.web.context.support.GenericWebApplicationContext@6eebc39e: startup date [Thu Sep 05 14:47:43 CST 2019]; root of context hierarchy