2

In PowerMockito, we can use the pattern "whenNew(MyClass).thenReturn(mockMyClass)" when someone wants to new an instance of MyClass, it will receive mockMyClass instead of the real instance.

Can we do similar things in ScalaMock or EasyMock? I spent whole day for this issue.

This is my scenario, I have two classes A and B. Inside class A, it will new an instance of class B.

Now I want to test class A, and when class A creates a new instance of class B, what I want is to return a mock object of class B (not the real class B).

In Java, I can handle this issue easily with PowerMock and JUnit, but I cannot do it in Scala.

Marco Dinh
  • 332
  • 2
  • 15

1 Answers1

0

For EasyMock, it is not directly possible. You need to use PowerMock. See here

Henri
  • 5,551
  • 1
  • 22
  • 29
  • Hello, thanks for you answer. In Java, I can use PowerMock to handle this issue easily, but I cannot do it in Scala. Do you know any other solution? – Marco Dinh Sep 15 '15 at 12:49
  • I don't do enough scala to have an answer. I would myself refactor just a little bit. Like moving the new into a dedicated method and then do a partial mock with EasyMock – Henri Sep 16 '15 at 14:42