saveAndFlush()没有保存到模拟仓库

huangapple go评论76阅读模式
英文:

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(&quot;Steph&quot;);
        repo.saveAndFlush(account);
    }

      @Test
      void list(){
        List&lt;Account&gt; 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&lt;Account&gt; list = Collections.singletonList(new Account())
when(repo.findAll().thenReturn(list);

// test something (for example the list is processed/manipulated by the controller

huangapple
  • 本文由 发表于 2020年10月9日 23:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64282857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定