springboot 测试 出错

 

测试代码

 

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FileTest {
    
    @Autowired
    private WebApplicationContext wac;
    
    private MockMvc mockMvc;
    
    @Before
    public void setup(){
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }
    
    @Test
    public void whencreateSuccess() throws Exception {
        //String content = "{\"password\":\"tom\",\"age\":1},\"username\":null}";
        String content = "{\"file\":\"a.bb\"}";
        String result = mockMvc.perform(MockMvcRequestBuilders.post("/upload")
                    .content(content)
                    .contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(MockMvcResultMatchers.status().isOk())
                //.andExpect(MockMvcResultMatchers.jsonPath("$.age").value("12"))
                .andReturn().getResponse().getContentAsString();
        System.out.println(result);
    }
}

 

执行报错

错误提示

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, 
you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

 

解决:

测试和主函数启动类所在包名一致就可以了

 

posted @ 2018-03-27 17:38  lyon♪♫  阅读(158)  评论(0编辑  收藏  举报