Spring boot 弹簧&x2B;Powermock+;JUnit从一个服务调用另一个服务-NullPointerException

Spring boot 弹簧&x2B;Powermock+;JUnit从一个服务调用另一个服务-NullPointerException,spring-boot,junit,mockito,powermock,Spring Boot,Junit,Mockito,Powermock,获取NullPointerException。不知道我做错了什么。任何帮助都将不胜感激 在调用时获取空指针 Teacher t = teacherService.getTeacherDetails(); 我进行了调试,发现teacherService为空。我不知道为什么它是空的,因为我已经在我的测试类中模拟了这个对象 StudentServiceTest.java @RunWith(PowerMockRunner.class) @PrepareForTest({StudentService.c

获取NullPointerException。不知道我做错了什么。任何帮助都将不胜感激

在调用时获取空指针

Teacher t = teacherService.getTeacherDetails();
我进行了调试,发现teacherService为空。我不知道为什么它是空的,因为我已经在我的测试类中模拟了这个对象

StudentServiceTest.java

@RunWith(PowerMockRunner.class)
@PrepareForTest({StudentService.class, TeacherService.class})
public class StudentServiceTest{

    @InjectMocks
    StudentService studentService;

    @InjectMocks
    TeacherService teacherService;

    @Mock
    private StudentRepository studentRepository;

    @Mock
    private TeacherRepository teacherRepository;

    @Test
    public void getStudentInformation() {
        Student student = new Student();
        Teacher teacher = new Teacher();

        when(studentRepository.getStudentDetails()).thenReturn(student);
        when(teacherRepository.getTeacherDetails()).thenReturn(teacher);

        Student student = studentService.getStudentInformaition();

    }
   private TeacherService teacherService;

   @Autowired
    public StudentService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public Student getStudentInformaition() {
        Teacher t = teacherService.getTeacherDetails();
        // some logic
        Student s = studentRepository.getStudentDetails();
       // some more logic
       return s;
    }
 public Teacher getTeacherDetails() {
        Teacher t = teacherRepository.getTeacherDetails();
        return t;
    }
StudentService.java

@RunWith(PowerMockRunner.class)
@PrepareForTest({StudentService.class, TeacherService.class})
public class StudentServiceTest{

    @InjectMocks
    StudentService studentService;

    @InjectMocks
    TeacherService teacherService;

    @Mock
    private StudentRepository studentRepository;

    @Mock
    private TeacherRepository teacherRepository;

    @Test
    public void getStudentInformation() {
        Student student = new Student();
        Teacher teacher = new Teacher();

        when(studentRepository.getStudentDetails()).thenReturn(student);
        when(teacherRepository.getTeacherDetails()).thenReturn(teacher);

        Student student = studentService.getStudentInformaition();

    }
   private TeacherService teacherService;

   @Autowired
    public StudentService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public Student getStudentInformaition() {
        Teacher t = teacherService.getTeacherDetails();
        // some logic
        Student s = studentRepository.getStudentDetails();
       // some more logic
       return s;
    }
 public Teacher getTeacherDetails() {
        Teacher t = teacherRepository.getTeacherDetails();
        return t;
    }
TeacherService.java

@RunWith(PowerMockRunner.class)
@PrepareForTest({StudentService.class, TeacherService.class})
public class StudentServiceTest{

    @InjectMocks
    StudentService studentService;

    @InjectMocks
    TeacherService teacherService;

    @Mock
    private StudentRepository studentRepository;

    @Mock
    private TeacherRepository teacherRepository;

    @Test
    public void getStudentInformation() {
        Student student = new Student();
        Teacher teacher = new Teacher();

        when(studentRepository.getStudentDetails()).thenReturn(student);
        when(teacherRepository.getTeacherDetails()).thenReturn(teacher);

        Student student = studentService.getStudentInformaition();

    }
   private TeacherService teacherService;

   @Autowired
    public StudentService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public Student getStudentInformaition() {
        Teacher t = teacherService.getTeacherDetails();
        // some logic
        Student s = studentRepository.getStudentDetails();
       // some more logic
       return s;
    }
 public Teacher getTeacherDetails() {
        Teacher t = teacherRepository.getTeacherDetails();
        return t;
    }

问题是这个代码

@InjectMocks
StudentService studentService;
将定义的模拟对象实例注入
studentService
实例,但
TeacherService
的实例不是模拟,因此不会作为模拟注入
studentService
实例

您应该将代码调整为以下内容:

@RunWith(PowerMockRunner.class)
@PrepareForTest({StudentService.class, TeacherService.class})
public class StudentServiceTest{

    @InjectMocks
    StudentService studentService;

    @Mock
    TeacherService teacherService;

    @Mock
    private StudentRepository studentRepository;

    @Test
    public void getStudentInformation() {
        Student student = new Student();
        Teacher teacher = mock(Teacher.class);

        when(studentRepository.getStudentDetails()).thenReturn(student);
        when(teacherService.getTeacherDetails()).thenReturn(teacher);
        when(teacher.getFoo()).thenReturn(???);

        Student student = studentService.getStudentInformaition();

    }

请注意,
teacherService
现在是一个模拟对象实例,不再需要
TeacherRepository
,因为您正在进行单元测试
StudentService
,那么您应该只将模拟注入到
StudentService
的实例中,包括
teacherService
的模拟实例,这有什么作用
getstudentinformation()
return
Student s
?@StalateofTuning是,updatedmy的目标是调用Student服务,该服务将调用教师服务并执行业务逻辑。如果我只是模拟教师服务,我的目标将无法实现。那么您就不是单元测试
StudentService
。如果您真的非常想走这条路,那么您需要避免
@InjectMocks
的魔力,并手动构建
StudentService
@SK的实例。您不应该在
teacherService
类中测试
teacherService
。顺便说一句,
@Autowired
是一个Spring的东西,在这里不起作用。PowerMockito将忽略该注释