我想模拟单例对象?这在scalamock-3中似乎是不可能的
我发现easyMock和powerMock可以模拟单例对象(来自https://github.com/fabura/scala-MockStaticObjects)
但是,我不能得到这项工作?有什么想法吗?
发布于 2016-02-05 02:41:59
如果单例对象有一个静态的.getInstance方法,它就很容易模拟。
您需要在测试类的顶部执行以下操作
@RunWith(PowerMockRunner.class) @PrepareForTest(Singleton.class)
然后模拟单例
mockStatic(Singleton.class); Singleton mockSingleton = mock(Singleton.class); when(Singleton.getInstance()).thenReturn(mockSingleton);
我相信您必须在"when“示例之前设置希望返回的单例所具有的任何值:when(mockSingleton.toString()).thenReturn("I'm a mock");
https://stackoverflow.com/questions/35148077
复制相似问题