使用PowerMock在JUnit Suite中进行共享静态初始化

时间:2013-11-22 18:53:00

标签: java unit-testing junit powermock

我正在使用一个旧的Java代码库的大型测试套件。简而言之,它使用DBUnit从本地硬盘上传静态只读数据集。目前,这是在每个测试级别上完成的,这意味着该套件的运行时间非常长。

我正在尝试在套件级别创建共享静态类。 (我们也没有定义合适的测试套件 - 我使用ClasspathSuite

制作了一个

另一个问题是,所有的测试都是使用@RunWith(PowerMockRunner.class) - 所以偶尔会出现类路径问题,这些问题会破坏我认为通常会解决的问题。

以下是一个不起作用的简单案例。


受测试的Java代码

代码库中的静态依赖

package com.somecorp.proj;
public class SomeDependency {
    public static String getStaticString() {
        // some resource intensive process we don't want running in unit tests
        return "real value";
    }
}

受测试类1

package com.somecorp.proj;

public class UnderTest {

    public String getIt() {
        return "Here is the value: " + SomeDependency.getStaticString();
    }
}

考试中的课程2

package com.somecorp.proj;

public class AlsoUnderTest {
    public String getTheThing() {
        return "some other value using it: " + SomeDependency.getStaticString();
    }
}

JUnit代码

使用init方法的代码我想在套件运行开始时仅运行ONCE

package com.somecorp.proj.testClasses;

public class StaticTestClassRequiringInitialization {

    private static String testString;

    public static void init() {
        // Some expensive stuff
        System.out.println("EXPENSIVE INITIALIZATION");
        testString = "a test string";
    }

    public static String getTestString() {
        return testString;
    }
}

测试1

package com.somecorp.proj;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.somecorp.proj.testClasses.StaticTestClassRequiringInitialization;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeDependency.class)
public class TestUnderTest {

    @Before
    public void setUp() {
        PowerMockito.mockStatic(SomeDependency.class);
        PowerMockito.when(SomeDependency.getStaticString()).
            thenReturn(StaticTestClassRequiringInitialization.getTestString());
    }

    @Test
    public void testGetIt() {
        UnderTest ut = new UnderTest();
        assertEquals(
            "Here is the value: a test string",
            ut.getIt()
        );
    }
}

测试2

package com.somecorp.proj;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.somecorp.proj.testClasses.StaticTestClassRequiringInitialization;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SomeDependency.class)
public class TestAlsoUnderTest {

    @Before
    public void setUp() {
        PowerMockito.mockStatic(SomeDependency.class);
        PowerMockito.when(SomeDependency.getStaticString()).
            thenReturn(StaticTestClassRequiringInitialization.getTestString());
    }

    @Test
    public void testGetTheThing() {
        AlsoUnderTest ut = new AlsoUnderTest();
        assertEquals(
            "some other value using it: a test string",
            ut.getTheThing()
        );
    }
}

测试套件

package com.somecorp.proj;

import static org.junit.extensions.cpsuite.SuiteType.RUN_WITH_CLASSES;
import static org.junit.extensions.cpsuite.SuiteType.TEST_CLASSES;

import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.BeforeSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters;
import org.junit.extensions.cpsuite.ClasspathSuite.SuiteTypes;
import org.junit.runner.RunWith;

import com.somecorp.proj.testClasses.StaticTestClassRequiringInitialization;

@RunWith(ClasspathSuite.class)
@SuiteTypes({RUN_WITH_CLASSES, TEST_CLASSES})
@ClassnameFilters({".*Test.*"})
public class ProjectJUnitSuite {

    @BeforeSuite
    public static void setUpBeforeSuite() {
        StaticTestClassRequiringInitialization.init();
    }

}

JAR详细信息

  • powermock-的Mockito-1.4.12-full.jar
  • 的Mockito-全1.9.0.jar
  • cpsuite-1.2.6.jar
  • 了Javassist-3.16.1-GA.jar
  • 使用Junit 4.8.1

测试失败的痕迹(特别是不是错误 - 失败)(对于一个测试......第二个测试非常相同):

org.junit.ComparisonFailure: expected:<...her value using it: [a test string]> but was:<...her value using it: [null]>
    at org.junit.Assert.assertEquals(Assert.java:123)
    at org.junit.Assert.assertEquals(Assert.java:145)
    at com.somecorp.proj.TestAlsoUnderTest.testGetTheThing(TestAlsoUnderTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.junit.extensions.cpsuite.ClasspathSuite.run(ClasspathSuite.java:196)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

如何让每个套件运行一次这个共享的静态初始化程序,并且可以从我所有启用Powermock的单元测试中引用它?

1 个答案:

答案 0 :(得分:1)

通过在引用@PowerMockIgnore的所有测试类上使用StaticTestClassRequiringInitialization注释,我只进行了一次初始化。

在这种情况下,特别是在两个JUnit测试类中添加以下注释都可以解决问题。

@PowerMockIgnore("com.somecorp.proj.testClasses.StaticTestClassRequiringInitialization")

之前我曾经尝试过这个问题,但最初没有尝试过,因为我最初通过的论据是:

  • 太宽 - 需要模拟的类不再由powermock类加载器加载,因为我将整个包传递给PowerMockIgnore
  • 不够广泛 - 存在一些比上述示例更复杂的依赖项,要求我忽略其他一些类。结果是一些奇怪的类加载不匹配错误。

这在所有情况下都不起作用(特别是如果StaticTestClassRequiringInitialization使用在测试中也被模拟的类,它将无法工作),但在这种情况下它确实有用。

我还对PowerMockAgent进行了一些调查,以避免使用PowerMockRunner,因此许多相关的Classloading问题完全出现,但是测试中的生产代码*需要抑制静态初始化,因此它不是启动器。

*抱歉不分享完整的源代码,这就是为大型专有代码库提问的危险。