java.lang.AssertionError:期望的状态:< 200>但是:< 404>

时间:2016-09-13 06:05:47

标签: java unit-testing spring-mvc mockito

我知道这个问题的帖子太多了。我试图实现所有但不适合我。请帮助我,为什么我收到此错误java.lang.AssertionError:状态预期:< 200>但是:< 404>

我尝试实现MediaType.APPLICATION_JSON 注释@EnableWebMvc 但是没有工作

我是否还需要包含标题才能使其正常工作?请让我知道

我写的代码是:

控制器类:

@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类:

@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信息: 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>

在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>

1 个答案:

答案 0 :(得分:0)

即使启用WebMvc,您也需要扫描控制器以自动注册控制器&amp; url mappings.component-scan扫描包以在应用程序上下文中查找和注册bean。

<context:component-scan base-package="com.mycompany.xyz" />

我怀疑你的上下文xml中缺少组件扫描。如此,请在beanRefContext.xml中添加此语句。

相关问题