SpringBoot UT编写

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {aaa.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public abstract class BaseTest {
    @Autowired
    protected MockMvc mockMvc;

    /**
     * MockBean不管有没有用Mockito代理方法,都不会调用真实的方法,有返回值的会返回null。可以理
     * 解为所有方法mock。
     * 也可以使用SpyBean,和MockBean区别是mock部分方法,仅mock的方法返回mock的结果,其余方法真实
     * 调用。
     */
    @MockBean
    private Pap pap;

    @SpyBean
    private Pep pep;

    @Test
    public void aaaa() throws Exception {
        try (MockedStatic<LoginUtil> mocked = Mockito.mockStatic(LoginUtil.class)) {
            // 构造mock返回值
            mocked.when(LoginUtil::getLongEmployeeId).thenReturn("user2");
            Mockito.when(pap.insert(2)).thenReturn(1);
            MockHttpServletResponse response = mockMvc.perform(post("/user").content(
                    (TestUtils.readFile("1.json")))
                .accept(MediaType.APPLICATION_JSON)
                .contentType(MediaType.APPLICATION_JSON)).andReturn().getResponse();
            Object result = TestUtils.getRestData(response, "queryDataEntityId3");
            JSONAssert.assertEquals(TestUtils.readFile("2.json"),
                JSONObject.toJSONString(result, SerializerFeature.WriteMapNullValue), false);
        }
    }
}
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
import org.springframework.data.redis.core.convert.ReferenceResolverImpl;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 测试启动类
 *
 * @since 2023/03/06
 */
@MapperScan(basePackages = {"com.xxxx"},
    annotationClass = Mapper.class)
@EnableScheduling
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
@ComponentScan(basePackages = {"com.huawei.*"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@SpringBootConfiguration
public class AuthmsgApplicationTest {
    private static final Logger LOG = LoggerFactory.getLogger(AuthmsgApplicationTest.class);

    @MockBean
    protected RedisConfig redisConfig;

    @MockBean
    protected RedisAutoConfiguration redisAutoConfiguration;

    @MockBean
    protected ReferenceResolverImpl referenceResolver;

    @MockBean
    protected RedisKeyValueAdapter redisKeyValueAdapter;

    public static void main(String[] args) {
        SpringApplication.run(AuthmsgApplicationTest.class, args);
        LOG.info("Start AuthmsgApplicationTest Success");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值