You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot Repository单元测试出现NullPointerException求助

SpringBoot Repository 新增User功能JUnit测试空指针问题排查

测试SpringBoot Repository向数据库新增User的功能时,执行JUnit测试抛出NullPointerException。排查发现UserService中的UserRepository注入为空,已给UserRepository添加@Repository注解,也尝试在UserService上加@SpringBootApplication,但问题依旧,求排查原因。

相关代码

User实体类

package org.example;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(nullable=false, unique=true)
    private int creditCardNumber;

    @Column(nullable=false, unique=true)
    private String email;

    @Column(nullable=false)
    private String password;
}

UserRepository

package org.example;

import org.example.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long>{

}

UserService(原代码存在问题,已标注修正点)

// 关键缺失:原代码没有package声明,必须补上
package org.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

// 错误:不要在Service类上加@SpringBootApplication,这是主启动类专属注解
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    // 建议修改:去掉id参数,因为id配置了自增策略,无需手动传入
    public User createUser(int creditCardNumber, String email, String password) {
        User user = new User(null, creditCardNumber, email, password);
        return userRepository.save(user);
    }
}

UserTest

package org.example;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UserTest {

    @Autowired
    private UserService userService;

    @Test
    public void testCreateUser() {
        User newUser = userService.createUser(203952, "john@john.com", "siofd");
        assertEquals("john@john.com", newUser.getEmail());
    }
}

问题排查与修复方案

  1. 修正@SpringBootApplication的位置
    @SpringBootApplication是Spring Boot主启动类的注解,不能加在Service类上。需要单独创建一个主启动类:

    package org.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    同时删除UserService上的@SpringBootApplication注解,只保留@Service。

  2. 补全UserService的package声明
    原UserService代码缺少package org.example;,导致Spring组件扫描无法识别这个类,这是依赖注入失败的核心原因之一,必须补上。

  3. 调整自增ID的使用逻辑
    User实体的id配置了数据库自增策略,手动传入id会触发主键冲突,同时违背自增设计。修改createUser方法,不再接收id参数,创建User对象时传入null,让数据库自动生成主键值。

  4. 确认依赖与扫描范围

    • 确保项目依赖中包含spring-boot-starter-test,保证JUnit测试能正常加载Spring上下文。
    • @SpringBootApplication默认扫描当前包及其子包,所有相关类(实体、Repository、Service、测试类)都要放在org.example包或其子包下,确保Spring能扫描到所有组件。

内容的提问来源于stack exchange,提问作者qpwoeiruty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 00:15:28