2024-06-10
记录springboot测试
重点需要 @RunWith(SpringRunner.class) 和 @SpringBootTest 否则无法测试类无法加载bean。出现空指针异常
依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringBoot 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
import com.alibaba.fastjson.JSONObject;
……
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DmeoTest {
@Test
public void testMyServiceMethod() {
ComUtil cimUtil= new ComUtil();
Jsonobj result =cimUtil.test();
System.out.println("kkjson->" + JSONObject.toJSONString(result));
}
@Autowired
private UserService userService;
@Test
public void testGet() {
List<User> list= userService.getUserList();
System.out.println("kkjson->" + JSONObject.toJSONString(list));
}
}