【问题标题】:How to mock object with constructor that takes a Class?如何使用带有类的构造函数来模拟对象?
【发布时间】:2011-06-24 23:23:22
【问题描述】:

这是测试:

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。这就是错误的来源。

【问题讨论】:

  • 删除 CollaboratorToBeMocked( java.lang.Class.class ) 构造函数时会发生什么?那它有用吗?
  • 你的意思是,当我删除其他构造函数时......是的,如果我使用 InjectedIntoCollaborator 删除构造函数,它会起作用

标签: java unit-testing junit mockito powermock


【解决方案1】:

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

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

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

希望对您有所帮助。 :)

【讨论】:

  • 这个答案对我有用。我遇到了具有通用 T 的 ResponseEntity 类的情况。 public ResponseEntity(MultiValueMap headers, HttpStatus statusCode) public ResponseEntity(T body, HttpStatus statusCode) 如果您使用以下语法: PowerMockito.whenNew(ResponseEntity.class ).withArguments(headers, statusCode).thenReturn(responseEntity);它不起作用,但是 PowerMockito.whenNew(ResponseEntity.class).withParameterTypes(MultiValueMap.class, HttpStatus.class).withArguments(headers, statusCode).thenReturn(responseEntity);作品
【解决方案2】:

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

如果超类有几个 构造函数可以告诉 PowerMock 仅抑制特定的 一。假设您有一个名为 ClassWithSeveralConstructors 有 一个采用String 的构造函数 和另一个接受一个 int 作为参数,你只想要 抑制 String 构造函数。 您可以使用 suppress(constructor(ClassWithSeveralConstructors.class, String.class)); 方法。

发现于http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior

这不是你想要的吗?

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

【讨论】:

  • 你是对的。我的压制是一团糟。尽管如此,它仍然不起作用我使用suppress(constructor(CollaboratorToBeMocked.class, InjectedIntoCollaborator.class));
  • 嗯,我的测试很糟糕。一旦我修好了测试课,测试就变绿了。所以,现在的问题是压制。可能是一个错误...
  • 如果你有一个完整的例子,但仍然看不到预期的结果,我绝对应该向项目提交错误报告。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
相关资源
最近更新 更多