[英]java.lang.AssertionError: Status expected:<200> but was:<404>
I know there are so many similar posts for this question. 我知道这个问题有很多类似的帖子。 I tried to implement all but not working for me. 我尝试实现所有功能,但没有为我工作。 Please help me why I am getting this error java.lang.AssertionError: Status expected:<200> but was:<404> 请帮助我为什么我会收到此错误java.lang.AssertionError:预期状态:<200>,但是是:<404>
I tried to implement MediaType.APPLICATION_JSON Annotation @EnableWebMvc But not working 我尝试实现MediaType.APPLICATION_JSON注释@EnableWebMvc,但无法正常工作
Do I need to include headers also to get that working? 我是否还需要包含标题才能使其正常工作? Please let me know 请告诉我
Code that I have written is: 我写的代码是:
Controller class: 控制器类:
@EnableWebMvc
@Controller
public class Controller {
@RequestMapping(value = "/app/data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseData> DataInquiry(
@RequestBody RequestData requestData,
@RequestHeader(value = Constants.ID, required = false) String transactionId) {
//Do some action
return new ResponseEntity<ResponseData>(responseData, headers, HttpStatus.OK);
}
ControllerTest class: ControllerTest类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:spring/beanRefContext.xml"})
@WebAppConfiguration
public class ControllerTest{
public static final MediaType APPLICATION_JSON_UTF8 = new MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(),
Charset.forName("utf8"));
private MockMvc mockMvc;
@Autowired
WebApplicationContext wac;
ObjectMapper mapper;
AnnotationMethodHandlerAdapter adapter;
MockHttpServletRequest request;
MockHttpServletResponse response;
@Before
public void setUp() {
System.out.println("Before method execution in CommonInquiryControllerTest class ");
//this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).dispatchOptions(true).build();
adapter = new AnnotationMethodHandlerAdapter();
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
mapper = new ObjectMapper();
}
@Test
public void InquiryDataTest() throws Exception, JsonProcessingException
{
RequestData anObject = new RequestData();
anObject.setId("1234");
anObject.setQualifier("someData");
mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
String requestJson=ow.writeValueAsString(anObject );
assertNotNull(anObject.getId());
assertNotNull(anObject.getQualifier());
ResultActions resultActions = mockMvc
.perform(post("/app/data")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsBytes(requestJson)));
resultActions.andExpect(status().isOk());
//This will print the response JSON string
resultActions.andDo(MockMvcResultHandlers.print());
Assert.assertEquals(200, response.getStatus());
}
xml Info: beanContext.xml xml信息:beanContext.xml
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:encryption="http://www.jasypt.org/schema/encryption"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.jasypt.org/schema/encryption
http://www.jasypt.org/schema/encryption/jasypt-spring3-encryption-1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<import resource="classpath:core-application-context.xml"/>
<import resource="classpath:region-context.xml"/>
<jee:jndi-lookup id="somedataSourceid" jndi-name="some name" proxy-interface="javax.sql.DataSource"/>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- a PlatformTransactionManager is still required -->
<bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
</beans>
In region-context.xml: 在region-context.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- Import this context file and pass it the requisite properties and a Data Source named dataSource -->
<context:component-scan base-package="com.java.geek"/>
</beans>
Even though you Enable WebMvc, you need to scan your controller to auto register controller & url mappings.component-scan scans packages to find and register beans within the application context. 即使启用WebMvc,也需要扫描控制器以自动注册控制器和URL映射.component-scan扫描软件包以在应用程序上下文中查找和注册bean。
<context:component-scan base-package="com.mycompany.xyz" />
I doubt component scanning is missing in your context xml.If so Add this statement in your beanRefContext.xml. 我怀疑上下文xml中没有组件扫描。如果是这样,请在beanRefContext.xml中添加此语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.