mockito

Mocking Sftp class skip method call

你说的曾经没有我的故事 提交于 2020-02-23 03:54:25
问题 public class SFTP { public Map<Report, TransferStatus> transfer(List<Report> reports) { //testing logic here } private ChannelSftp channelSftp; private Session session; private TransferStatus send(File file) { connect(); send(stream, file.getName()); } private void send(FileInputStream stream, String name) throws SftpException, IOException { channelSftp.put(stream, fileNameWithId, new SftpLogMonitor(), ChannelSftp.OVERWRITE); stream.close(); } private void connect() throws JSchException { if

Manually instantiating the @InjectMock annotated field

烈酒焚心 提交于 2020-02-21 13:13:46
问题 I have gone through some of the blogs in order to understand the basics of how Mockito annotations work. However I am facing a doubt as to when does one go for manually instantiating the field annotated with @InjectMocks i.e @InjectMocks A a = new A(); And when does one rely on MockitoAnnotations.initMocks() functionality to do the same : @InjectMocks A a; Does this depend on the JunitTestRunner that we employ to run the test cases or is it dependent on the Mockito framework version? 回答1: It

How to mock a autowired list of Spring beans?

半腔热情 提交于 2020-02-20 06:35:30
问题 I've read plenty of articles about how to mock Spring's bean and their autowired fields. But there is nothing I could find about autowired lists of beans. Concrete problem I've a class called FormValidatorManager . This class loop through several validators which implements IFormValidator . @Component public class FormValidatorManager implements IValidatorManager { @Autowired private List<IFormValidator> validators; @Override public final IFieldError validate(ColumnDTO columnToValidate,

PowerMock throws NoSuchMethodError (setMockName)

不羁岁月 提交于 2020-02-17 10:54:46
问题 I'm trying to mock a constructor using PowerMockito but every time I run the test I get the following error: java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.setMockName(Lorg/mockito/mock/MockName;)Lorg/mockito/internal/creation/settings/CreationSettings; at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:107) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) at org

SpringBoot 单元测试利器——Mockito

孤者浪人 提交于 2020-02-08 20:20:49
Mockito 是一种 Java mock 框架,他主要是用来做 mock 测试的,他可以模拟任何 Spring 管理的 bean、模拟方法的返回值、模拟抛出异常...等,在了解 Mockito 的具体用法之前,得先了解什麽是 mock 测试 1. 什么是 mock 测试? mock 测试就是在测试过程中,创建一个假的对象,避免你为了测试一个方法,却要自行构建整个 bean 的依赖链 像是以下这张图,类 A 需要调用类 B 和类 C,而类 B 和类 C 又需要调用其他类如 D、E、F 等,假设类 D 是一个外部服务,那就会很难测,因为你的返回结果会直接的受外部服务影响,导致你的单元测试可能今天会过、但明天就过不了了 而当我们引入 mock 测试时,就可以创建一个假的对象,替换掉真实的 bean B 和 C,这样在调用B、C的方法时,实际上就会去调用这个假的 mock 对象的方法,而我们就可以自己设定这个 mock 对象的参数和期望结果,让我们可以专注在测试当前的类 A,而不会受到其他的外部服务影响,这样测试效率就能提高很多 2. Mockito 简介 说完了 mock 测试的概念,接下来我们进入到今天的主题,Mockito Mockito 是一种 Java mock 框架,他主要就是用来做 mock 测试的,他可以模拟任何 Spring 管理的 bean、模拟方法的返回值

How to test(mock) object that uses external API (Jama Software)

空扰寡人 提交于 2020-02-08 02:36:53
问题 I encountered a problem while trying to test Class, which uses external API - Jama Software . Let's say there is a ClassA . It has 2 methods,that I want to test using JUnit. Both methods have ClassB as an argument. ClassB is belonging to another API. I can see it's methods but the implementation details are not available. (compiled code) . The question is: How can I mock this classes to make TestClass isolated? I know that in Mockito there is an option to hardcode responses. Is there any

mockito简要教程

随声附和 提交于 2020-02-03 04:37:44
1 mockito概述 Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with a clean & simple API. Mockito doesn’t give you hangover because the tests are very readable and they produce clean verification errors. Mockito是一个非常不错的模拟框架。 它使您可以使用干净简单的API编写漂亮的测试。 Mockito不会给您带来麻烦,因为这些测试可读性强,并且会产生清晰的验证错误。 官方说明-how简要版本 mockito官方文档-详细版本 特性和动机github说明 其特性如下: mock具体的类和接口; 小注释语法糖-@Mock 验证错误是干净的-单击堆栈跟踪以查看测试中失败的验证; 单击异常原因以导航到代码中的实际交互。 堆栈跟踪始终是干净的。 允许按顺序进行灵活的验证(例如:按顺序进行验证,而不是每次交互都进行验证) 支持精确次数和最少一次的验证 使用参数匹配器(anyObject(),anyString()或refEq()进行基于反射的相等匹配)的灵活验证或存根

org.mockito.exceptions.misusing.NotAMockException on InjectMocks object

会有一股神秘感。 提交于 2020-02-03 02:11:12
问题 I'm trying to mock the return value from a method but I'm getting NotAMockException . @InjectMocks private MyService myService; @Mock private OtherServiceUsedInMyServiceAsAutowired otherServiceUsedInMyServiceAsAutowired; Inside MyService I have a method called myMethod() and I want to return dummy object when this method is called. doReturn(someDummyObject).when(myService).myMethod(any(), any(), any()); And at that point I'm getting the error. What am I doing wrong? Full error: org.mockito

mockito: Is there a way of capturing the return value of stubbed method?

房东的猫 提交于 2020-02-01 12:58:37
问题 If I mock a method to return a new instance of some object, how can I capture the returned instance? E.g.: when(mock.someMethod(anyString())).thenAnswer(new Answer() { Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); return new Foo(args[0]) } }); Obviously, I can have a field of type Foo and inside answer set it to the new instance, but is there a nicer way? Something like ArgumentCaptor? 回答1: Looks like you want to

mockito: Is there a way of capturing the return value of stubbed method?

不羁的心 提交于 2020-02-01 12:57:14
问题 If I mock a method to return a new instance of some object, how can I capture the returned instance? E.g.: when(mock.someMethod(anyString())).thenAnswer(new Answer() { Object answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); Object mock = invocation.getMock(); return new Foo(args[0]) } }); Obviously, I can have a field of type Foo and inside answer set it to the new instance, but is there a nicer way? Something like ArgumentCaptor? 回答1: Looks like you want to