在JUnit到TestNG之间切换后,我的测试不起作用

时间:2014-06-26 18:21:19

标签: java unit-testing testng powermock

我无法理解为什么我的代码无法执行TestNG测试。在我使用JUnit测试(具有相应的JUnit依赖性)而不是TestNG的同时,一切正常。即使我在我的IDEA中使用适当的测试框架(TestNG)重新创建了所有测试类,它也不能很好地工作。实际上问题出在PowerMock

这是我测试的简单代码

public class Employee {
    public static int count(){
        throw new UnsupportedOperationException();
    }
}

我正在测试的服务类

public class EmployeeService {
   public int getEmployeeCount(){
       return Employee.count();
   }
}

最后是我的测试代码

@PrepareForTest(Employee.class)
public class EmployeeServiceTestNGTest {

    @Test
    public void should_return_count_of_employee_using_the_domain_class() {
        PowerMockito.mockStatic(Employee.class);
        PowerMockito.when(Employee.count()).thenReturn(1000);

        EmployeeService employeeService = new EmployeeService();
        Assert.assertEquals(1000, employeeService.getEmployeeCount());
    }
}

我的pom.xml包含所有依赖项:

    <properties>
        <powermock.version>1.5.5</powermock.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-testng</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-testng-common</artifactId>
            <version>1.4.11</version>
        </dependency>
    </dependencies>

我的堆栈跟踪

java.lang.UnsupportedOperationException
    at il.arri.powermock.example.Employee.count(Employee.java:9)
    at il.arri.powermock.example.EmployeeServiceTestNGTest.should_return_count_of_employee_using_the_domain_class(EmployeeServiceTestNGTest.java:14)

UPD

我将测试类更改为

@PrepareForTest(Employee.class) 公共类EmployeeServiceTestNGTest扩展了PowerMockTestCase {

@Test
public void should_return_count_of_employee_using_the_domain_class() {
    PowerMockito.mockStatic(Employee.class);
    PowerMockito.when(Employee.count()).thenReturn(1000);

    EmployeeService employeeService = new EmployeeService();
    Assert.assertEquals(1000, employeeService.getEmployeeCount());
}

@ObjectFactory
public IObjectFactory getObjectFactory() {
    return new PowerMockObjectFactory();
}

}

但它仍有错误:

org.testng.TestNGException: 
An error occurred while instantiating class il.arri.powermock.example.EmployeeServiceTestNGTest: null
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:398)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:115)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:200)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:160)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:575)
    at org.testng.SuiteRunner.init(SuiteRunner.java:159)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:113)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.NullPointerException
    at org.powermock.core.classloader.MockClassLoader.addClassesToModify(MockClassLoader.java:133)
    at org.powermock.modules.testng.internal.PowerMockClassloaderObjectFactory.newInstance(PowerMockClassloaderObjectFactory.java:81)
    at org.powermock.modules.testng.PowerMockObjectFactory.newInstance(PowerMockObjectFactory.java:42)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
    ... 26 more

UPD(解决方案):

当我删除此依赖项

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-testng-common</artifactId>
            <version>1.4.11</version>
        </dependency>

所有作品都是绿色的:)

2 个答案:

答案 0 :(得分:1)

坦率地说,要解决我的问题,我需要更改一个具有冗余依赖关系的pom.xml文件,例如

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-testng-common</artifactId>
    <version>1.4.11</version>
</dependency>

我还需要添加PowerMockObjectFactory支持

@ObjectFactory
public IObjectFactory getObjectFactory() {
    return new PowerMockObjectFactory();
}

最后一件事是从PowerMockTestCase类延伸。

@PrepareForTest(Employee.class)
public class EmployeeServiceTestNGTest extends PowerMockTestCase {...}

只有在这种情况下它才能正常工作并且绿色。

答案 1 :(得分:0)

是不是应该抛出UnsupportedOperationException?

你告诉它在静态count()方法中抛出异常。

您可以让单元测试期望抛出特定的异常。