我已经在提供REST API的应用程序中的类中添加了验证,并且我试图重组单元测试以使用MockMvc,以便调用验证。我已经看过几本有关设置方法的教程,但是每当我尝试调用其中一个API时,都会收到404空响应,并且日志文件显示“找不到/ xxxxx的处理程序方法”。
XML配置文件或测试类中可能只缺少一些东西,但我一直无法弄清。
测试代码:
//Let's say it's your binding adapter from ViewModel
fun signUp() {
if (check validation logic) {
// Produce errors
}
// Further successful stuffs
}
控制器:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring_test.xml")
@TestPropertySource("classpath:test.properties")
@WebAppConfiguration
public class ClientControllerTest {
@Autowired
private Validator validator;
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
private static final String baseUrl = "http://localhost:8080";
@Mock
StorageProvider storageProvider;
@Mock
ClientCriteria clientCriteria;
@Mock
UserPermissionChecker userPermissionChecker;
@Before
public void setUp() throws BusinessException {
MockitoAnnotations.initMocks(this);
String identityUserToken = "TEST USER";
Mockito.when(userPermissionChecker.checkPermissions(Mockito.any())).thenReturn(identityUserToken);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testMockMvc() throws Exception {
String clientId = "1";
String channel = "internet";
String market = "canada";
String transactionStyle = "blah";
String featureSet = "blah_blah";
Client requestedClient = new Client(clientId, channel, market, transactionStyle, featureSet);
ServletContext sc = webApplicationContext.getServletContext();
assertNotNull(sc);
assertTrue(sc instanceof MockServletContext);
ResultActions ra = mockMvc.perform(post("/myapibase/v1/clients")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.convertObjectToJsonBytes(requestedClient))
);
ra.andDo(print());
XML配置
@RestController
@RequestMapping("/myapibase/v1")
public class ClientController {
@Autowired
private Validator validator;
@Autowired
private StorageProvider storageProvider;
@Autowired
private UserPermissionChecker userPermissionChecker;
private String baseUrl;
private static Logger log = LoggerFactory.getLogger(ClientController.class.getName());
@Autowired
public ClientController(String baseUrl) {
this.baseUrl = baseUrl;
}
@RequestMapping(value = "/clients", method = RequestMethod.POST)
public ResponseEntity<?> addClient(@RequestBody @Valid Client clientToAdd, @RequestHeader HttpHeaders headers, @RequestParam Map<String, String> queryParams) {
String methodName = "addClient";
...
}
Application.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config />
<tx:annotation-driven/>
<mvc:annotation-driven/>
<!--<mvc:default-servlet-handler/>-->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:ValidationMessages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="messageAccessor" class="org.springframework.context.support.MessageSourceAccessor">
<constructor-arg index="0" ref="messageSource"/>
</bean>
</beans>
日志输出:
@Configuration
@EnableAutoConfiguration(exclude = MetricRepositoryAutoConfiguration.class)
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableServiceFoundation
@ComponentScan(basePackages = { "com.myapp.controllers", "com.myapp.models", "com.myapp.storage", "com.myapp.managers",
"com.myapp.txyz", "com.myapp.util", })
public class Application {
...
}