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