Spring-Boot Junit Test Rest调用异常

时间:2018-06-06 13:10:04

标签: rest spring-boot junit controller

在线

mockMvc.perform(MockMvcRequestBuilders.post("/v1/api/lineitems/") 

我得到例外:

  

org.springframework.web.util.NestedServletException:Request   处理失败;嵌套异常是java.lang.NullPointerException

我无法识别这个问题,非常感谢任何建议或帮助。

这是我的Junit课程:

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = FsaApp.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
//@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LineitemControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();

    }
@Test
    @Transactional
    @Rollback(true)
    public void testPostControllerSave() throws Exception 
    {   
        String b="{\"unit\": \"PERCENTAGE\","
    +"\"code\": \"PQR\","
    +"\"status\": \"ACTIVE\","
    +"\"approvalStatus\": \"AWAITING_AUTHORIZATION\","
    +"\"annualization\": 1,"
    +"\"makerComments\": \"creating lineitem\","
    +"\"checkerComments\": null,"
    +"\"finGroup\": null,"
    +"\"langMap\": ["
        +"{"
            +"\"code\": \"hi\","
            +"\"value\": \"Hindi\""
            +"},"
            +"{"
            +"\"code\": \"en\","
            +"\"value\": \"English\""
            +"}"
            +"],"
    +"\"type\": \"HEADER\"}";

        mockMvc.perform(MockMvcRequestBuilders.post("/v1/api/lineitems/")
                .contentType(MediaType.APPLICATION_JSON)
                .content(b))
                //.accept(MediaType.APPLICATION_JSON))
                .andExpect(jsonPath("$.id").exists())
                .andExpect(jsonPath("$.unit").exists())
                .andExpect(jsonPath("$.code").exists())
                .andExpect(jsonPath("$.unit").value("PERCENTAGE"))
                .andExpect(jsonPath("$.code").value("PQR"))
                .andDo(print());

        //JSONAssert.assertEquals("", "", false);
    }
}

0 个答案:

没有答案