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

    解决:

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

  • 相关阅读:
    How do I access arcobjects from python?
    Win7 打开或关闭Windows功能 窗口空白 解决方案(ZZ)
    解释什么叫工作
    电脑城奸商最怕顾客知道的十条经验
    25岁前你要学会放下的八样东西
    必看十大电影
    SQL Server 中查询非中文,非英文,非数字的特殊列
    CHARINDEX 和 PATINDEX
    主流开源数据库的技术特点点评
    information_schema.routines与sysobjects
  • 原文地址:https://www.cnblogs.com/lyon91/p/8658805.html
Copyright © 2011-2022 走看看