为什么@MockBean 不模拟我的目标存储库 class (returns null)?
Why @MockBean does not mock my target repository class (returns null)?
这是我要测试的class
@Component
public class PermissionCheck {
@Autowired
private MyEntityRepository myEntityRepository;
public boolean hasPermission(int myEntityID) {
MyEntity myEntity = myEntityRepository.findById(myEntityId);
return myEntity != null;
}
}
这里是测试class
@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)
上面的line1和line2就是指这两行
MyEntity myEntity = myEntityRepository.findById(myEntityId);
assertTrue(this.permissionCheck.hasPermission(0));
并且使用调试器我看到当从 PermissionCheckTests.shouldHasPermission
输入 PermissionCheck.hasPermission
时,存储库字段
@Autowired
private MyEntityRepository myEntityRepository;
为空。
我创建这些 classes 是通过参考来自不同地方的其他现有代码,并且没有真正理解注释的方式(部分原因是 运行ning 没时间),所以如果有人不仅可以告诉我如何修复,还可以告诉我为什么我错了,我将不胜感激!
编辑:
我按照@Nikolas Charalambidis 的建议进行了更改(谢谢!),所以我的 PermissionCheck
class 现在看起来完全像
@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="")}
稍微搜索了一下,从this SO answer,我觉得我不应该@Autowired PermissionCheck
,因为它是一个class,而不是一个接口。
这是否意味着我必须创建一个接口,@Autowired 并让 PermissionCheck
实现它?这对我来说似乎是多余的,因为我不明白为什么我需要这样一个界面。有没有一种方法可以在不创建我将专门用于此目的的新界面的情况下使其工作?
class PermissionCheck
的实例未正确注入。测试使用 Spring 容器的方式与生产代码相同。以下行不会注入 myEntityRepository
.
this.permissionCheck = new PermissionCheck();
Spring: NullPointerException occurs when including a bean 部分回答了您的问题。你需要 @Autowire
东西。
Spring 测试上下文也不例外。只要 @MockBean
MyEntityRepository
作为模拟 bean 存在,PermissionCheck
将使用模拟的 class 以标准方式自动装配 [=27] =] 在测试范围 .
中的现有 bean
@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));
}
}
这是我要测试的class
@Component
public class PermissionCheck {
@Autowired
private MyEntityRepository myEntityRepository;
public boolean hasPermission(int myEntityID) {
MyEntity myEntity = myEntityRepository.findById(myEntityId);
return myEntity != null;
}
}
这里是测试class
@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)
上面的line1和line2就是指这两行
MyEntity myEntity = myEntityRepository.findById(myEntityId);
assertTrue(this.permissionCheck.hasPermission(0));
并且使用调试器我看到当从 PermissionCheckTests.shouldHasPermission
输入 PermissionCheck.hasPermission
时,存储库字段
@Autowired
private MyEntityRepository myEntityRepository;
为空。
我创建这些 classes 是通过参考来自不同地方的其他现有代码,并且没有真正理解注释的方式(部分原因是 运行ning 没时间),所以如果有人不仅可以告诉我如何修复,还可以告诉我为什么我错了,我将不胜感激!
编辑:
我按照@Nikolas Charalambidis 的建议进行了更改(谢谢!),所以我的 PermissionCheck
class 现在看起来完全像
@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="")}
稍微搜索了一下,从this SO answer,我觉得我不应该@Autowired PermissionCheck
,因为它是一个class,而不是一个接口。
这是否意味着我必须创建一个接口,@Autowired 并让 PermissionCheck
实现它?这对我来说似乎是多余的,因为我不明白为什么我需要这样一个界面。有没有一种方法可以在不创建我将专门用于此目的的新界面的情况下使其工作?
class PermissionCheck
的实例未正确注入。测试使用 Spring 容器的方式与生产代码相同。以下行不会注入 myEntityRepository
.
this.permissionCheck = new PermissionCheck();
Spring: NullPointerException occurs when including a bean 部分回答了您的问题。你需要 @Autowire
东西。
Spring 测试上下文也不例外。只要 @MockBean
MyEntityRepository
作为模拟 bean 存在,PermissionCheck
将使用模拟的 class 以标准方式自动装配 [=27] =] 在测试范围 .
@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));
}
}