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

307 Views Asked by At

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!

1

There are 1 best solutions below

1
Jasper-M On BEST ANSWER

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