mock静态final方法调用使用powermock返回null

jdzmm42g  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(350)

我正在尝试为学生类编写一个测试用例,用于getnumber方法。并且,它必须调用注册类的最终静态方法。在我的junit中,我使用powermock来模拟静态调用,但是它没有返回注册示例,而是返回一个空值。

public class Student{

 public static String getNumber(){
            Enrollment enroll =   Enrollment.getInstance("xyz");
         return "123";
  }
}

public class Enrollment {
  public static final Enrollment getInstance(String id){
    return new Enrollment;
  }
}

测试:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Student.class, Enrollment.class})
public class StudentTest{

@Test
public void testGetNumber(){
        PowerMockito.mockStatic(Enrollment.class);
        Enrollment enrollment = PowerMockito.mock(Enrollment.class);
        PowerMockito.when(Enrollment.getInstance(Mockito.anyString())) .thenReturn(enrollment);
            Student.getNumber(); 
        Mockito.verify(Enrollment.getInstance(Mockito.anyString())); 
   }
}

有人能帮忙吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题