JUnit taunts Mockito, EasyMock, etc.

I am trying to make fun of an object method inside a class that I am testing.

for instance

class ClassToTest { public doSomething () { SomeObject a = new SomeObject (); a.doSomethingElse (); } } 

Is there a way to mock the methods of the variable "a"? I would like doSomethingElse to do nothing during testing. I am currently using Mockito, but I am open to any mocking frameworks.

thanks

+4
source share
6 answers

Yes, there is a way as shown in the following JMockit test:

 public void testDoSomething(final SomeObject mock) { new ClassToTest().doSomething(); new Verifications() {{ mock.doSomethingElse(); }}; } 

No need to check refactoring code to use wrappers, DI, etc .; just scoff at what you need to taunt.

+2
source

It is impossible to mock the link "a" when it is declared as a local variable, as in your case. You might consider introducing a dependency on SomeObject, for example. as a parameter to the doSomething method. That way, you can inject the SomeObject layout into your test instead.

One of the benefits of dependency injection is to increase test ability .

+2
source

With some refactoring, of course, it's possible:

 class SomeObject { public void doSomethingElse() { } } class ClassToTest { private final SomeObject someObject; public void doSomething() { someObject.doSomethingElse(); } public ClassToTest(SomeObject someObject) { this.someObject = someObject; } } class Test { @Test public void testDoSomething() { SomeObject someObject = Mockito.mock(SomeObject.class); new ClassToTest(someObject).doSomething(); Mockito.verify(someObject, Mockito.atLeastOnce()).doSomethingElse(); } } 
+2
source

I believe that you can use the EasyMock class extensions for EasyMock 2.5 or earlier, and it seems to be included in 3.0. See this part of the previous page for information about what you are trying to do. However, I personally have not tried to do this, so I don’t know how well it will work.

+1
source

If you need a new instance in every call, I would suggest refactoring as follows:

 class ClassToTest { public doSomething () { SomeObject a = getInstance(); a.doSomethingElse (); } protected SomeObject getInstance() { return new SomeObject(); } } 

You can then create a test class that extends ClassToTest by overriding the getInstance () method, one of which provides a mock object.

This, of course, is only viable if you are okay with the expansion of the getInstance () method, so I do not recommend it if the class is part of the public API. If so, consider supplying a factory class using dependency injection.

0
source
 class ClassToTest { private SomethingElseInterface somethingElseDoer ; public ClassToTest(SomethingElseInterface somethingElseDoer) { this.somethingElseDoer = somethingElseDoer; } public doSomething () { somethingElseDoer.doSomethingElse(); } } 

And where do you use it:

 SomethingElseInterface somethingElseDoer = ...; // in a test, this is where you mock it ClassToTest foo = new ClassToTest(somethingElseDoer); // inject through constructor foo.doSomething(); 
0
source

All Articles