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

    解决:

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

  • 相关阅读:
    Flume基础(一):概述
    Hive高级(2):优化(2) 表的优化
    ospf命令
    Verizon 和某 BGP 优化器如何在今日大范围重创互联网
    BGP数据中心鉴别方法
    多线BGP鉴定
    mpls ldp neighbor 和loopbak
    ospf默认路由
    ospf
    ubuntu cloud init获取元数据失败
  • 原文地址:https://www.cnblogs.com/lyon91/p/8658805.html
Copyright © 2011-2022 走看看