Spring RestTemplate invoking webservice with errors and analyze status code

前端 未结 5 917
执笔经年
执笔经年 2020-12-07 17:39

I designed a webservice to perform a task if request parameters are OK, or return 401 Unauthorized HTTP status code if request parameters are wrong or empty.

I\'m us

5条回答
  •  一整个雨季
    2020-12-07 18:31

    You can use spring-test. It's much easier:

    @WebAppConfiguration
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:your-context.xml")
    public class BasicControllerTest {
    
            @Autowired
            protected WebApplicationContext wac;
            protected MockMvc mockMvc;
    
            @Before
            public void setUp() throws Exception {
            mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
            }
    
            @Test
            public void testUnauthorized(){
    
            mockMvc.perform(MockMvcRequestBuilders
                                .post("your_url")
                                .param("name", "values")
            .andDo(MockMvcResultHandlers.print())
            .andExpect(MockMvcResultMatchers.status().isUnauthorized()
            .andExpect(MockMvcResultMatchers.content().string(Matchers.notNullValue()));
            }
    }
    

提交回复
热议问题