页面未找到使用Spring的单元测试
如何修复此错误?
一切顺利,我记得没有任何变化。
但现在...... 404代码。
测试
class WebIntegrityTest extends BaseWebSpecification {
private List controllers = []
//setup
controllers << new PaymentController()
def "Charge"() {
when:
ResultActions result = performGet("/perform", controllers)
then:
result.andExpect(isOk)
}
}
基
abstract class BaseWebSpecification extends BaseSpecification {
protected static final ResultMatcher isOk//...
protected static ResultActions performGet(String url, Object... controllers) {
buildMockMvc(controllers).perform(MockMvcRequestBuilders.get(url))
}
private static MockMvc buildMockMvc(Object... controllers) {
MockMvcBuilders.standaloneSetup(controllers).build()
}
}
弹簧
4.3.2.RELEASE
的web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/web-context.xml
</param-value>
</context-param>
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
控制器
package com.abc.payments.controllers;
@RestController
@RequestMapping("/perform")
public class PaymentController {
@ResponseBody
@RequestMapping(method = GET)
public String perform() throws IOException, URISyntaxException {
return "{\"test\":\"test\"}";
}
}
上下文
<context:component-scan base-package="com.abc.payments.controllers"/>
<mvc:annotation-driven/>