springboot测试类

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

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容