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));
    }

    /**
     * 新增
     * @throws Exception
     */
    @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\": 20\n" +
                "}";
        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")));
    }

    /**
     * 分页列表
     * @throws Exception
     */
    @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 (默认)。
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值