spring boot项目测试controller和service的方法

测试controller

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MainApplication.class,MockServletContext.class})
public class ReindexTest {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReindexTest.class);


    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void testDeleteAllHistory() {
        try {
            MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/es/deleteHistory").param("userId",
                    "419"))
                    .andReturn();
            LOGGER.info(mvcResult.getResponse().getContentAsString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试service

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApplication.class)
@TestPropertySource(locations = "classpath:application-test.properties")
@ActiveProfiles("test")
public class ApplicationServiceTest {

    @Autowired
    private ApplicationService applicationService;

    @Test
    public void testSearchAppList() {
        List<Application> list = applicationService.searchAppList("公积金", 1, 10);
        System.out.println(list.size());
    }
}

注意:@TestPropertySource注解用来指定其加载的properties文件,默认是application.properties

参考:java - Override default Spring-Boot application.properties settings in Junit Test - Stack Overflow

https://stackoverflow.com/questions/45659316/spring-boot-tests-cant-find-test-properties

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值