Service调试
在@Service类上Ctrl+Shift+T,创建新测试,勾选编写测试的方法;
在测试类上添加注解:
package com.yile.app.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.*;
@ActiveProfiles("dev")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoServiceTest {
@Autowired
DemoService demoService;
SysUser user = null;
@Before
public void setUp() {
user = new SysUser();
user.setId(1);
}
@Test
public void test() {
Object obj = demoService.test();
Assert.notNull(obj, "请求失败");
}
}
Controller测试添加注解@AutoConfigureMockMvc
参考:https://blog.csdn.net/s418683338/article/details/103711858