英文:
saveAndFlush() not saving to mock repo
问题
以下是翻译好的代码部分:
我正在尝试为一个API编写测试,对于Spring和Mockito都是新手。当我使用saveAndFlush时,似乎没有命中存储库。这个测试通过了,但实际上应该失败,因为列表中应该有一个账户。你知道我在这里漏掉了什么吗?
public class AccountTest {
@InjectMocks
private AccountController underTest;
@Mock
private AccountRepository repo;
private Account account = new Account();
@BeforeEach
void setUp() throws Exception{
MockitoAnnotations.initMocks(this);
account.setAccountNumber((long) 6666);
account.setName("Steph");
repo.saveAndFlush(account);
}
@Test
void list(){
List<Account> mockList = repo.findAll();
when(underTest.list()).thenReturn(mockList);
assert mockList != null;
assertEquals(0, mockList.size());
}
}
英文:
I am trying to write tests for an API, just new to Spring and Mockito. When i saveAndFlush it doesn't seem to hit the repo. This test passes when it should fail as there should be 1 account in the list. Any idea what i am missing here?
public class AccountTest {
@InjectMocks
private AccountController underTest;
@Mock
private AccountRepository repo;
private Account account = new Account();
@BeforeEach
void setUp() throws Exception{
MockitoAnnotations.initMocks(this);
account.setAccountNumber((long) 6666);
account.setName("Steph");
repo.saveAndFlush(account);
}
@Test
void list(){
List<Account> mockList = repo.findAll();
when(underTest.list()).thenReturn(mockList);
assert mockList != null;
assertEquals(0, mockList.size());
}
}
答案1
得分: 1
在这种情况下没有仓库:Mock 是一个假装是真实仓库的对象,但不提供任何功能。
Mockito 保证调用模拟对象(就像它是真实仓库一样),但所有方法都不执行任何操作(函数返回 null 或空列表,每个 void 方法都不执行任何操作)。
在你的示例中,findAll() 正确地返回一个空列表。
想法是配置你的模拟对象来模拟一些行为
# 当调用 findById() 方法(任何参数)时返回给定的对象
when(repo.findById(isA(Long.class))).thenReturn(new Account());
并验证它的行为是否符合预期
# 验证 save() 方法被调用一次
verify(repo, times(1)).save(isA(Account.class));
在你特定的示例中,如果你的测试需要一个非空列表,你可以模拟 findAll()
# 返回一个包含一个元素的列表
List<Account> list = Collections.singletonList(new Account());
when(repo.findAll()).thenReturn(list);
// 测试一些内容(例如列表是否被控制器处理/操作)
英文:
There is no repository in this case: the Mock is an object pretending to be the real repository but without providing any functionality.
Mockito guarantees that the mock is called (as if it was the real repository) but all methods do nothing (functions returns null or empty lists, every void method does nothing).
In your example findAll() correctly returns an empty list.
The idea is to configure your mock to simulate some behaviour
# return a given object when the findById() method (any param) is called
when(repo.findById(isA(Long.class))).thenReturn(new Account());
and to verify it behaves as expected
# verify save() method is called once
verify(repo, times(1)).save(isA(Account.class));
In your specific example you could mock findAll() if your test needs a list which is not empty
# return a list with one element
List<Account> list = Collections.singletonList(new Account())
when(repo.findAll().thenReturn(list);
// test something (for example the list is processed/manipulated by the controller
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论