zoukankan      html  css  js  c++  java
  • 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

    解决:

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

  • 相关阅读:
    Java框架之Mybatis(一)
    Java框架之Hibernate(四)
    Java框架之Hibernate(三)
    递归与分治
    散列
    绪论
    系统的分类(二)
    系统的定义与分类(一)
    Guess My Number 游戏
    2.5 随机数的生成
  • 原文地址:https://www.cnblogs.com/lyon91/p/8658805.html
Copyright © 2011-2022 走看看