如何模拟Akka Actor作用域内的静态函数?

问题描述 投票:0回答:1

我有一个actor,它调用了一个Helper的静态方法,我想对其进行模拟。

public class ExampleActor extends AbstractActor {
    public Receive createReceive() {
        .match(CachedFile.class, cachedFile -> {
            UploadFileHelper.makeRequest(cachedFile.getContent());
            return true;
        });
    }
}

我试过模拟它

@RunWith(PowerMockRunner.class)
@PrepareForTest(UploadFileHelper.class)
public class ExampleActorTest {

    @Test
    public void testCreateFile() {
        new TestKit(system) {{
            PowerMockito.mockStatic(UploadFileHelper.class);
            PowerMockito.when(
                UploadFileHelper.makeRequest(any())
            ).thenReturn(true);

            exampleActor.tell(cachedFile, getRef());
        }}
    }
}

但由于任何原因,真正的方法被调用,而不是被模拟的方法。

为了在Akka Actor上下文中模拟静态方法,我应该做什么改变吗?

java unit-testing junit akka powermockito
1个回答
0
投票

当我使用mockStatic时,mockStatic在

"main"@1 in group "main": RUNNING 

是可以的,但不能在

"AkkaTestKit-akka.actor.default-dispatcher-3"@2,614 in group "main": RUNNING

只要用 CallingThreadDispatcher 当你创建你的道具。

final Props props = Props.create(ExampleActor.class).withDispatcher(CallingThreadDispatcher.Id());

对我来说,这很有效。

© www.soinside.com 2019 - 2024. All rights reserved.