gpt4 book ai didi

spring - 显示与@MockBean 注释模拟的交互

转载 作者:行者123 更新时间:2023-11-28 20:28:10 28 4
gpt4 key购买 nike

在 Spring 应用程序中,可以使用默认 Autowiring 的模拟 bean 编写测试,并且可以使用常用的 Mockito 方法进一步自定义。为此,使用了@MockedBean 注释。但是,当只使用 Mockito 而没有使用 Spring 时,可以配置模拟以打印所有交互,请参阅 How to use Mockito to show all invocations on a mock .模拟 bean 也可以进行这种调试吗?

最佳答案

没有 Sprint native 方法,但可以使用 mockingDetails(<some mock>) Mockito 中检索 MockingDetails 的方法模拟然后打印交互。将调试放在 @After 注释的方法中以每次显示它,即使测试失败也是如此。

import static org.mockito.Mockito.mockingDetails;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import com.fasterxml.jackson.core.JsonProcessingException;

@RunWith(SpringRunner.class)
@SpringBootTest(
public class MyTest {
@MockBean
LoginDAO loginDaoMock;

@Test
public void xyz() {
// some test
}

@After
public void showInteractions() {
System.out.println(mockingDetails(loginDaoMock).getInvocations());
}
}

关于spring - 显示与@MockBean 注释模拟的交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44882842/

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