Powermock not able to mock static methods when using scala, junit

ⅰ亾dé卋堺 提交于 2019-12-22 18:16:27

问题


I have used Powermock previously with java and junit. I have been successfully been able to mock static methods like in the following example:

@PrepareForTest({ TimeHelper.class, MainApp.class })
@RunWith(PowerMockRunner.class)
public class TestSuite {
    @Before
    public void setUp() throws IOException {
     PowerMockito.mockStatic(TimeHelper.class);
     Mockito.doReturn("2015-01-01 00:00:00").when(TimeHelper.getUnixTime());
    }
}

However, the same doesn't seem to work when translated to scala. Note that the 'getUnixTime' method that we call below is also implemented in scala.

@PrepareForTest(Array( classOf[TimeHelper], classOf[MainApp] ))
@RunWith(classOf[PowerMockRunner])
class TestSuite {
    @Before
    def setUp() {
     PowerMockito.mockStatic(classOf[TimeHelper]);
     Mockito.doReturn("2015-01-01 00:00:00").when(TimeHelper.getUnixTime());
    }
}

When running the test implemented in scala, I seem to get the value returned by the test, instead of the mocked value. Can someone point out what I am missing here?

EDIT: Here's the signature of the getUnixTime function in scala:

object TimeHelper{
    def getUnixTime(): String = {
      //logic
    }
}

来源:https://stackoverflow.com/questions/43678415/powermock-not-able-to-mock-static-methods-when-using-scala-junit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!