java 如何使用带类的构造函数模拟对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4959489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mock object with constructor that takes a Class?
提问by Belun
This is the test:
这是测试:
import static junit.framework.Assert.assertTrue;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest( {ClassUnderTesting.class} )
public class ClassUnderTestingTest {
@Test
public void shouldInitializeMocks() throws Exception {
CollaboratorToBeMocked mockedCollaborator = mock(CollaboratorToBeMocked.class);
suppress(constructor(CollaboratorToBeMocked.class, InjectedIntoCollaborator.class));
whenNew(CollaboratorToBeMocked.class)
.withArguments(InjectedAsTypeIntoCollaborator.class)
.thenReturn(mockedCollaborator);
new ClassUnderTesting().methodUnderTesting();
assertTrue(true);
}
}
These are the classes :
这些是类:
public class ClassUnderTesting {
public void methodUnderTesting() {
new CollaboratorToBeMocked(InjectedAsTypeIntoCollaborator.class);
}
}
public class CollaboratorToBeMocked {
public CollaboratorToBeMocked(Class<InjectedAsTypeIntoCollaborator> clazz) {
}
public CollaboratorToBeMocked(InjectedIntoCollaborator someCollaborator) {
}
public CollaboratorToBeMocked() {
}
}
public class InjectedAsTypeIntoCollaborator {
}
public class InjectedIntoCollaborator {
}
This is the error :
这是错误:
org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're refering to.
Matching constructors in class CollaboratorToBeMocked were:
CollaboratorToBeMocked( InjectedIntoCollaborator.class )
CollaboratorToBeMocked( java.lang.Class.class )
Here comes the question: how can I make PowerMock figure out what constructor to look for?
问题来了:如何让 PowerMock 找出要查找的构造函数?
The problematic lineis the suppress
. That is where the error comes from.
有问题的行是suppress
. 这就是错误的来源。
回答by Smartmarkey
Perhaps it is too late for your question. I met it today and found the solution at the following url. Basically, you need to specify your argument type like.
也许你的问题为时已晚。我今天遇到了它,并在以下网址找到了解决方案。基本上,您需要指定您的参数类型。
whenNew(MimeMessage.class).**withParameterTypes(MyParameterType.class)**.withArguments(isA(MyParameter.class)).thenReturn(mimeMessageMock);
http://groups.google.com/group/powermock/msg/347f6ef1fb34d946?pli=1
http://groups.google.com/group/powermock/msg/347f6ef1fb34d946?pli=1
Hope it can help you. :)
希望它可以帮助你。:)
回答by Grzegorz Oledzki
I didn't know of PowerMock until you wrote your question, but did some reading and found this in their documentation. Still I am not really sure if that helps you:
在您写下您的问题之前,我不知道 PowerMock,但我阅读了一些资料并在他们的文档中找到了这一点。我仍然不确定这是否对您有帮助:
If the super class have several constructors it's possible to tell PowerMock to only suppress a specific one. Let's say you have a class called
ClassWithSeveralConstructors
that has one constructor that takes aString
and another constructor that takes anint
as an argument and you only want to suppress theString
constructor. You can do this using thesuppress(constructor(ClassWithSeveralConstructors.class, String.class));
method.
如果超类有多个构造函数,则可以告诉 PowerMock 只抑制一个特定的构造函数。假设您有一个名为的类
ClassWithSeveralConstructors
,该类 具有一个接受 a 的String
构造函数和另一个接受 anint
作为参数的构造函数, 而您只想取消String
构造函数。您可以使用该suppress(constructor(ClassWithSeveralConstructors.class, String.class));
方法执行此操作 。
found at http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior
在http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior 上找到
Isn't it the thing you wanted?
这不是你想要的吗?
EDIT: Now I see, you've already tried suppressing. But are you sure you got the suppress call right? Isn't the first argument of constructor()
supposed to be the class you would like to surpress the constructor in?
编辑:现在我明白了,你已经尝试压制了。但是你确定你得到了抑制调用吗?不constructor()
应该是第一个参数应该是你想抑制构造函数的类吗?