问题
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