简体   繁体   中英

Is it possible to mock / stub methods of the same test class with ScalaMock?

With Mockito and other testing frameworks there are usually ways to mock functionality of a method within the test class. I couldn't seem to get ScalaMock to accept the same way.

class A {
 def methodUnderTest()
 def methodUsedInMethodUnderTest()
}

Then in the test class I'm:

(A.methodUsedInMethodUnderTest _)
.expects.....
a.methodUnderTest shouldEqual ..

I know that if you are mocking / stubbing out the class and then calling the same functionality on a real instance this won't work. But there are workarounds by using mocks for both calls etc.

If this is the wrong approach, what would be the best way to test a method that uses other methods in the same test class? I thought that decoupling the methods was the best practice.

Thanks!

If I understand your question correctly, you can create a mock of A and then tell ScalaMock to execute the real implementation of methodUnderTest .

val aMock = mock[A]

when(aMock.methodUnderTest).thenCallRealMethod()
when(aMock.methodUsedInMethodUnderTest).thenReturn(someValue)

aMock.methodUnderTest shouldEqual someOtherValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM