簡體   English   中英

在JUnit和TestNG之間切換后,我的測試不起作用

[英]My tests don't work after switching between JUnit to TestNG

我不明白為什么我的代碼無法執行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>

所有作品均為綠色:)

坦白地說,要解決我的問題,我需要更改一個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 {...}

只有在這種情況下,它才能正常工作並呈綠色。

難道不應該拋出UnsupportedOperationException嗎?

您要告訴它在您的static count()方法中拋出該異常。

您可以讓單元測試期望將拋出特定的異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM