微服务 Test调试数据 显示空指针

在测试微服务的一个连通数据库的时候,出现了,在测试类 test里面 查询数据为空指针。
用dao层测试了,只能在Test类里面用初始化null才能跑动起来。

img

这是用的sonsumer里的控制层测试的
img

这是用dao层测试的

img

数据库的查询语句都还可以跑通,就是说用sql语句在Mysql里面查询 的话,可以查出数据来的。
配置文件中的Mysql名和密码都没有问题。

dao层是否交由spring管理 是否注入dao

spring:你new?我启动做了这么多东西,你直接new搞定啦?

你要用spring去启动容器,然后将所有的controller service mapper交由spring管理,而不是new 创建controller 如果你new创建知识创建了一个controller controller里面的依赖,spring没启动,不会帮你解决,所以空指针异常了。

你应该用 SpringBootTest 然后@Test 去启动,会自动帮你创建spring容器,管理各种bean. 然后你再从容器中获取controller 提供一段代码给你参考

@SpringBootTest
class WorkApplicationTests {

    @Autowired
    private ArticleMapper articleMapper;

    

    @Test
    void contextLoads() throws Exception {
        articleMapper.reportCurrentTime(1);
    }
}

这个类写在springtest下

img