Java 为什么@MockBean不模拟我的目标存储库类(返回null)?
这是我想测试的课程Java 为什么@MockBean不模拟我的目标存储库类(返回null)?,java,spring-boot,testing,mockito,Java,Spring Boot,Testing,Mockito,这是我想测试的课程 @Component public class PermissionCheck { @Autowired private MyEntityRepository myEntityRepository; public boolean hasPermission(int myEntityID) { MyEntity myEntity = myEntityRepository.findById(myEntityId); re
@Component
public class PermissionCheck {
@Autowired
private MyEntityRepository myEntityRepository;
public boolean hasPermission(int myEntityID) {
MyEntity myEntity = myEntityRepository.findById(myEntityId);
return myEntity != null;
}
}
这是考试班
@RunWith(SpringRunner.class)
public class PermissionCheckTests {
@MockBean
private MyEntityRepository myEntityRepository;
private PermissionCheck permissionCheck;
@Before
public void before() {
this.permissionCheck = new PermissionCheck();
}
@Test
public void shouldHasPermission() {
MyEntity myEntity = new MyEntity();
when(this.myEntityRepository.findById(any())).thenReturn(myEntity);
assertTrue(this.permissionCheck.hasPermission(0));
}
}
当我运行这个测试时,我得到了
java.lang.NullPointerException
at PermissionCheck.hasPermission(PermissionCheck.java:line1)
at PermissionCheckTests.shouldHasPermission(PermissionCheckTests.java:line2)
在上文中,第1行和第2行指的是这两行
MyEntity myEntity = myEntityRepository.findById(myEntityId);
使用调试器,我看到当从PermissionCheckTests.shouldAspermission
中输入PermissionCheck.hasPermission
时,存储库字段
@Autowired
private MyEntityRepository myEntityRepository;
是空的
我通过引用其他来自不同地方的现有代码创建了这些类,但没有真正理解如何修改注释(部分原因是时间不够),因此,如果有人不仅能告诉我如何修改,还能告诉我为什么我错了,我将非常感激
编辑:
我做了@Nikolas Charalambidis(谢谢!)建议的更改,所以我的权限检查类现在看起来完全一样
@RunWith(SpringRunner.class)
public class PermissionCheckTests {
@Autowired // you need to autowire
private PermissionCheck permissionCheck; // and it uses @MockBean dependency
@MockBean // if no such @MockBean exists
private MyEntityRepository myEntityRepository; // the real implementation is used
@Test
public void shouldHasPermission() {
MyEntity myEntity = new MyEntity();
when(this.myEntityRepository.findById(any())).thenReturn(myEntity);
assertTrue(this.permissionCheck.hasPermission(0));
}
}
但我得到了以下例外
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'PermissionCheckTests':
Unsatisfied dependency expressed through field 'permissionCheck';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'PermissionCheck' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true),
@org.springframework.beans.factory.annotation.Qualifier(value="")}
通过一点搜索,我觉得我不应该@AutowiredPermissionCheck
,因为它是一个类,而不是一个接口
这是否意味着我必须创建一个接口,@Autowired并让PermissionCheck
实现它?这对我来说似乎是多余的,因为我不明白为什么我需要这样一个接口。有没有一种方法可以让它在不创建新接口的情况下工作,而我将仅用于此目的?类的实例PermissionCheck
没有正确注入。测试以与生产代码相同的方式使用Spring容器。下面的行不会注入myEntityRepository
this.permissionCheck = new PermissionCheck();
部分回答了你的问题。你需要@Autowire
这个东西
Spring测试上下文也不例外。只要@MockBean
MyEntityRepository
作为模拟bean存在,那么PermissionCheck
将以标准方式自动连接,使用模拟类优先于测试范围中的现有bean
谢谢你的建议!但我仍然有错误。。。你能看一下问题的最新部分吗?我也是这么要求的
this.permissionCheck = new PermissionCheck();
@RunWith(SpringRunner.class)
public class PermissionCheckTests {
@Autowired // you need to autowire
private PermissionCheck permissionCheck; // and it uses @MockBean dependency
@MockBean // if no such @MockBean exists
private MyEntityRepository myEntityRepository; // the real implementation is used
@Test
public void shouldHasPermission() {
MyEntity myEntity = new MyEntity();
when(this.myEntityRepository.findById(any())).thenReturn(myEntity);
assertTrue(this.permissionCheck.hasPermission(0));
}
}