Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 莫基托:“我不知道;想要但未调用:[……]实际上,与此模拟没有任何交互;_Java_Unit Testing_Junit_Mockito - Fatal编程技术网

Java 莫基托:“我不知道;想要但未调用:[……]实际上,与此模拟没有任何交互;

Java 莫基托:“我不知道;想要但未调用:[……]实际上,与此模拟没有任何交互;,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,我看过StackOverflow上的帖子 需要但未调用:实际上,与此模拟没有任何交互 我确实按要求做了,但我还是遗漏了一些东西。你能帮我找到我缺少的东西吗 我的Java代码: public class AccountController { public ResponseEntity<AccountResponse> getAccountByAccountId( @RequestHeader("Authorization") final String

我看过StackOverflow上的帖子

需要但未调用:实际上,与此模拟没有任何交互

我确实按要求做了,但我还是遗漏了一些东西。你能帮我找到我缺少的东西吗

我的Java代码:

public class AccountController {

    public ResponseEntity<AccountResponse> getAccountByAccountId(
            @RequestHeader("Authorization") final String accountToken,
            @PathVariable("accountId") String accountId) {

        return accountHandler.apply(() -> {

            final AcccountResponse accountResponse = accountService
                    .getAccountByAccountId(accountId);

            return ResponseEntity.ok().body(accountResponse);

        });
    }
}

您的
accountHandler
是一个mock,这意味着
apply
将只执行您要它执行的操作。当您将它存根时,您没有让它调用传递给它的
供应商
,这就是为什么
getAccountByAccountId
从未被调用的原因

对于
AccountHandler
字段,最简单的方法可能是使用真实的
AccountHandler
而不是模拟


另一种方法是使用Mockito
Answer
使
apply
方法工作。

您的
accountHandler
是一个mock,这意味着
apply
将只执行您存根它要执行的操作。当您将它存根时,您没有让它调用传递给它的
供应商
,这就是为什么
getAccountByAccountId
从未被调用的原因

对于
AccountHandler
字段,最简单的方法可能是使用真实的
AccountHandler
而不是模拟

另一种方法是使用Mockito
Answer
使
apply
方法工作

@InjectMocks
private AccountController accountController;

@Mock
private AccountService accountService;

@Mock
private AccountHandler accountHandler;

@Captor
private ArgumentCaptor<Supplier<ResponseEntity<AccountResponse>>> responseAccount;

private static AccountResponse accountResponse;

@Before
public void setUp() {

    responseEntity = ResponseEntity.ok().body(accountResponse);
}

@Test
public void getAccountByAccountIdTest() {

    when(accountHandler.apply(responseAccount.capture())).thenReturn(responseEntity);
    when(accountService.getAccountByAccountId(accountId)).thenReturn(accountResponse);

    ResponseEntity<AccountResponse> accountResult = accountController.getAccountByAccountId(accountToken, accountId);

    assertNotNull(accountResult);
    assertEquals(HttpStatus.OK, accountResult.getStatusCode());
    assertSame(accountResponse, accountResult.getBody());
    verify(accountHandler).apply(responseAccount.capture());
    verify(accountService).getAccountByAccountId(accountId); // this fails and gives me error
}
Wanted but not invoked:
acccountService.getAccountByAccountId(

    "accountId"
);
Actually, there were zero interactions with this mock.