繁体   English   中英

如何使用采用 Class 的构造函数模拟 object?

[英]How to mock object with constructor that takes a Class?

这是测试:

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);
    }
}

这些是类:

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 {

}

这是错误:

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 )

问题来了:如何让 PowerMock 找出要寻找的构造函数?

有问题的行suppress 这就是错误的来源。

也许你的问题为时已晚。 我今天遇到了它,并在以下网址找到了解决方案。 基本上,您需要指定您的参数类型。

whenNew(MimeMessage.class).**withParameterTypes(MyParameterType.class)**.withArguments(isA(MyParameter.class)).thenReturn(mimeMessageMock); 

http://groups.google.com/group/powermock/msg/347f6ef1fb34d946?pli=1

希望它可以帮到你。 :)

在你写完问题之前我不知道PowerMock,但做了一些阅读并在他们的文档中找到了这个。 我仍然不确定这是否对你有所帮助:

如果超类具有多个构造函数,则可以告诉PowerMock仅抑制特定的构造函数。 假设你有一个名为ClassWithSeveralConstructors的类,它有一个构造函数,它接受一个String ,另一个构造函数接受一个int作为参数,你只想要压缩String构造函数。 你可以使用suppress(constructor(ClassWithSeveralConstructors.class, String.class));来做到这一点suppress(constructor(ClassWithSeveralConstructors.class, String.class)); 方法。

http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior上找到

这不是你想要的吗?

编辑:现在我明白了,你已经尝试过抑制。 但你确定你有抑制电话吗? constructor()的第一个参数不应该是你想要压制构造函数的类吗?

如果将 PowerMock 用于 EasyMock,您可以执行PowerMock.expectNew(CollaboratorToBeMocked.class, new Class[]{InjectedIntoCollaborator.class}, ...)其中Class[]是您期望调用的构造函数的参数类型。 这将解决构造函数之间的歧义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM