将多个类与PowerMock结合使用

问题描述

我正在尝试构建一种隐藏PowerMock的基础结构,以供其他程序员测试使用一种非常独特的体系结构的程序,该体系结构是旧代码和新代码的混合体。
为了做到这一点,我以最简洁的方式创建了一些类,让他们准备在此类程序中使用的模拟,但这意味着将所述模拟分离到不同的文件中,因此出现了我的问题。例如,我以以下方式获取代码

@RunWith(powermockrunner.class)
@PrepareForTest({SomeClass.class,AnotherClass.class}) // These are meant to be mocked in the current .java
public abstract class ClasstoExtendByActualTests {
    
    private void setup(Config config) {
        // Just for the sake of the example
        Foo foo = mock(Foo.class);
        mockStatic(SomeClass.class);
        when(SomeClass.someStaticmethod()).thenReturn(foo);

        // This is where the problem lies
        ClassthatPreparesOtherMocks mockPreparer = new ClassthatPreparesOtherMocks(config);
        mockPreparer.prepare();
    }

    public void runProgram(Config config) {
        setup(config);
        program.run();
    }

}

然后我有一个ClassthatPreparesOtherMocks,我不知道该如何使用,但这是该工作的一种方式:

// Same package
class ClassthatPreparesOtherMocks {
    final Config config;
    ClassthatPreparesOtherMocks(Config config) {
        this.config = config;
    }

    prepare() {
        Bar bar = new Bar();
        // Prepared in ClasstoExtendByActualTests,not in this class
        mockStatic(AnotherClass.class);
        when(AnotherClass.staticmethod()).thenReturn(bar);
    }
}

因此,我目前面临的工作选择是将所有内容都放在一个我很讨厌的大类中,或者通过让ClassthatPreparesOtherMocks需要在{{ 1}},我也不喜欢。

我想知道的是,是否可以以某种方式在ClasstoExtendByActualTests中使用PrepareForTest批注来在其中创建模拟,而不是在ClassthatPreparesOtherMocks类中准备那些类。
我尝试使用ClasstoExtendByActualTests中的注释无济于事,也没有使用ClassthatPreparesOtherMocks,以防万一,但这是行不通的。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)