栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Junit测试运行出现No tests were found

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Junit测试运行出现No tests were found

单元测试代码
@SpringBootTest
@AutoConfigureMockMvc
public class TrialQuestionTest {

    @Autowired
    private MockMvc mockMvc;

    @Value("${test.token}")
    private String token;
    @Value("${test.language}")
    private String language;

    @Test
    void contextLoads() throws Exception {
        //新增
        Long id = add();
        //分页列表
//        pageList();
//        //修改
//        update(id);
//        //查看详情
//        getDetail(id);
//        //批量删除
//        delete(Collections.singletonList(id));
    }

    
    @Test
    Long add() throws Exception {
        String body = "{n" +
                "  "questionDescribe": "This is a test question",n" +
                "  "questionOption": "A,B,C,D",n" +
                "  "questionType": 2,n" +
                "  "sort": 20n" +
                "}";
        String content = mockMvc.perform(MockMvcRequestBuilders.post("/trialQuestion")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(body)
                        .header("TOKEN", token).header("accept-language", language)
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(print())
                .andReturn().getResponse().getContentAsString();
        Map map = (Map) JSONObject.parseObject(content, RestData.class).getData();
        return Long.parseLong(String.valueOf(map.get("id")));
    }

    
    @Test
    void pageList() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/trialDatabase/page")
                        .param("current", "1")
                        .param("size", "30")
//                        .param("filterRule", "1")
//                        .param("filterValue","Gale")
//                        .param("providerId", "41")
                        .contentType(MediaType.APPLICATION_JSON)
                        .accept(MediaType.APPLICATION_JSON)
                        .header("TOKEN", token).header("accept-language", language))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(print());
    }
  1. 执行contextLoads()方法的时候是没问题的。
  2. 当想单独执行add()方法时就出现了No tests were found这个错误。
  3. 然后我试着将add()方法返回值改成void,执行成功。
  4. 又试了一下将方法定义为private,同样报错。
由此可得出:
  1. @Test注解的单元测试方法 不能有返回值 ,要用 void 。
  2. 方法定义为 private 的也不行,必须为 public (默认)。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1013818.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号