【问题标题】:PowerMock @SuppressStaticInitializationFor doesn't work for nested class inside the test classPowerMock @SuppressStaticInitializationFor 不适用于测试类中的嵌套类
【发布时间】:2017-12-01 06:25:33
【问题描述】:

我对代码库中广泛使用的基类进行了更改,添加了一个静态初始化块。我现在正在检查我们所有的单元测试,试图通过添加使它们工作

@SuppressStaticInitializationFor("com.mycompany.WidelyUsedBaseClass")

这在某些情况下是有效的,但我无法让它与具有扩展 WidelyUsedBaseClass 的内部类的测试类一起使用。 (不要问我为什么存在这些嵌套类;我不确定为什么标准模拟还不够。无论如何,我不想尝试重构它,因为我不完全相信我理解测试逻辑使用这些内部类。)

这是我的意思的一个例子:

@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.mycompany.WidelyUsedBaseClass") // <-- This isn't working! I can still see <clinit> in the exception when the test fails...
public class SomeTestsFromAnotherTeam
{
    // test logic...
    // more test logic...
    Stub myStub = new Stub();
    // more test logic...

    private class Stub extends WidelyUsedBaseClass
    {
        // some other logic...
    }
}

我认为问题出现在new Stub() 通话中。

【问题讨论】:

    标签: java unit-testing powermock powermockito


    【解决方案1】:

    嗯,我无法弄清楚到底发生了什么,但我确实找到了解决问题的方法。

    将内部类移出封闭类后,测试通过。我只是将它放在同一个文件中的SomeTestsFromAnotherTeam 类之后,并将其设为私有:

    public class SomeTestsFromAnotherTeam
    {
        // ...
    }
    
    class Stub extends WidelyUsedBaseClass
    {
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      相关资源
      最近更新 更多