gpt4 book ai didi

java - 我无法使用 Mockito 模拟存储库

转载 作者:行者123 更新时间:2023-12-01 18:12:40 24 4
gpt4 key购买 nike

我正在尝试模拟存储库,但它不起作用(在它工作之前)。

@Mock
public UserRepository mockedUserRepository;

....

@InjectMocks
ClassDAOImpl daoImplSUT;

....

@Test
public void shouldTest() {
Mockito.when(mockedUserRepository.findAll()).thenReturn(operators);

List<User> users= daoImplSUT.getAllUsers();
}

我不知道为什么不起作用。当我调试时,我可以检查模拟存储库是否为空。然后我得到一个 NullPointerException。

你能帮我吗?非常感谢。

最佳答案

您没有初始化模拟。

  1. JUnit 5:在setUp之前添加MockitoAnnotations.initMocks(this)每个测试或测试类中的@ExtendWith(MockitoExtension.class)注释
  2. JUnit 4:在setUp之前添加MockitoAnnotations.initMocks(this)每个测试或测试中的 @RunWith(MockitoJUnitRunner.class) 注释类

关于java - 我无法使用 Mockito 模拟存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60437077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com