powermock + easymock + testng用于测试最终课程

时间:2014-07-11 18:11:36

标签: java unit-testing testng easymock powermock

在我的pom.xml中使用这些依赖项

<dependency>
         <groupId>org.powermock</groupId>
         <artifactId>powermock-api-easymock</artifactId>
         <version>1.5.5</version>
    </dependency>
    <dependency>
         <groupId>org.powermock</groupId>
         <artifactId>powermock-module-test-easymock-testng</artifactId>
         <version>1.5.5</version>
    </dependency>
    <dependency>
         <groupId>org.powermock</groupId>
         <artifactId>powermock-core</artifactId>
         <version>1.5.5</version>
    </dependency>

这是我的代码,其中Dispute是final类型。

 @PrepareForTest(Dispute.class)
        public void testProcessChargebackUpdates() {
            final List<BraintreeGateway> credentials = new LinkedList<BraintreeGateway>();
            credentials.add(credential);

    //      Dispute dispute = control.createMock(Dispute.class);
            Dispute dispute = PowerMock.createMock(Dispute.class);
}

Gives无法实例化最终的课程。

1 个答案:

答案 0 :(得分:1)

首先你需要这些依赖:

<dependency>
         <groupId>org.powermock</groupId>
         <artifactId>powermock-module-test-easymock-testng</artifactId>
         <version>1.5.5</version>
    </dependency>
    <dependency>
         <groupId>org.powermock</groupId>
         <artifactId>powermock-module-testng</artifactId>
         <version>1.4.9</version>
    </dependency><dependency>
         <groupId>org.powermock</groupId>
         <artifactId>powermock-core</artifactId>
         <version>1.5.5</version>
    </dependency>

然后你需要在你的班级中使用@Prepare注释,并将最终的类作为嘲笑 以及要在类中定义的getObjectFactory方法。

@PrepareForTest(Dispute.class)
public void testProcessChargebackUpdates() {
    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }
}