Java应用编写Junit测试使用Maven编译时没有执行单元测试

7 篇文章 0 订阅
7 篇文章 0 订阅

测试用例如下:

package com.jack.apple.user.controller;

import com.jack.apple.user.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
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;

import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
@ActiveProfiles(value = {"test"})
public class AppleUserControllerTest {
    protected MockMvc mockMvc;


    @BeforeEach
    void setUp() {
        //
        mockMvc = MockMvcBuilders
                .webAppContextSetup((WebApplicationContext) SpringUtils.getContext())
                .build();
    }

    @Test
    void mockRestfulGetUserByIdTest() throws Exception {
        MockHttpServletRequestBuilder mockRequestBuilder = MockMvcRequestBuilders
                .get("/users/getUserInfo/1")
                .accept(MediaType.APPLICATION_JSON);

        // 发送请求
        ResultActions resultActions = mockMvc.perform(mockRequestBuilder);
        // 请求状态
        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        // 请求结果
        MvcResult mvcResult = resultActions.andReturn();
        MockHttpServletResponse response = mvcResult.getResponse();
        String responseContent = response.getContentAsString();
        log.info("response info: {}", responseContent);
        assertTrue(StringUtils.isNotEmpty(responseContent));
    }

}

测试依赖

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
</dependency>

maven构建明显如下:

mvn clean package -e

构建输出信息如下:

 从输出信息可以看到测试用例数为零.

查看一下maven-surefire-plugin插件的使用介绍。官网地址: 

https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

显示引用该插件依赖后可正常执行单元测试

 <plugins>
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
     </plugin>
</plugins>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值