Java Mybaitis plus遇到java.lang.NullPointerException

2023年 10月 5日 63.2k 0

发生情景

在测试类测试mybaits时候,报了空指针异常,如下图

标题

image.png

对应Test类代码如下

package com.example.demo;

import com.example.demo.service.UserService;
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.test.context.junit4.SpringRunner;


@SpringBootTest
public class ServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testGetCount(){
        long count = userService.count();
        System.out.println(count);
    }

}

解决方案:

1. maven再编译一次

看到有人建议再编译一次,因为可能刚写的编译器还没加载到,试试去

image.png

还是失败

2. 在测试类添加@RunWith(SpringRunner.class)

这回成功了~

image.png

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论