15

I am interested in Mocking objects in a JUnit test-suite, however I have only come across mocking frameworks that use dependency injection to inject mock objects. However, I would like to be able to mock classes/functions without having to inject that mock object, along the lines of @patch() in python.

Trivial example:

//dependency injection
public String bar(Foo foo) {
    return foo.foo(); //just pass in mock Foo object
}
//.... onto test code
Foo mockedFoo = <Mocked Foo object>;
String response = bar(mockedFoo);
assertEqual(response, <mockedFoo return value>);


//case I am looking for
public String bar() {
    Foo foo = new Foo(); //how to create mock object here?
    return foo.foo(); //or simply how to mock a single function?
}
//... onto test code
<force the Foo class or foo method to be mocked from here without touching bar() source code>
String response = bar();
assertEqual(response, <mocked response>);
5
  • stackoverflow.com/questions/10895605/… Commented Dec 19, 2014 at 1:08
  • Use a library, e.g. Mockito. Or in simpler cases, extend Foo or even better, implement the same interface as Foo to get a simple mock. Commented Dec 19, 2014 at 1:11
  • Mike, from looking through Mockito, it still appears that dependencies have to be injected. Please see above for edit to increase clarity. Commented Dec 19, 2014 at 1:40
  • @Kamoor1982, the author of that post appears to be asking the opposite of what I am asking. Commented Dec 19, 2014 at 1:42
  • Anyone knows of a solution with c# instead of java? Commented Aug 6, 2015 at 14:13

2 Answers 2

4

You can use Powermock to instrument the class under test to return a mock when new is called.

Powermock mock constructor tutorial

your code would look like this:

RunWith(PowerMockRunner.class)
@PrepareForTest( Bar.class )
public class BarTest {


@Test
public void test(){
   Foo mockedFoo = createMock(Foo.class);
   //set up mockedFoo here
   ...

   //This will make a call  to new Foo() inside Bar.class
   //return your mock instead of a real new one
   expectNew(Foo.class).andReturn(mockedFoo);

   ...
   replay(mockedFoo, File.class);

   Bar bar = new Bar();
   String response = bar.bar();

   assertEqual(response, <mocked response>);

   verify(mockedFoo, File.class);


}

}
Sign up to request clarification or add additional context in comments.

1 Comment

if I am not wrong, expectNew is only implemented for use with EasyMock, not with Mockito. For Mockito, use whenNew instead.
2

Simply put, you could mock Foo like this

public String bar() {
    Foo foo = Mockito.mock(Foo.class);
    return foo.foo();
}

The problem with this though is that foo.foo() will essentially do nothing as you haven't defined what #foo() should return when we call the mocked version. Using a more complete example you could do something like this:

class MyTest {

    Foo mockedFoo = Mockito.mock(Foo.class);

    @Before
    public void setUp() throws Exception {
        Mockito.when(mockedFoo.foo()).thenReturn("This is mocked!");
    }

    @Test
    public void testMock() {
        String returnedFoo = mockedFoo.foo();
        Assert.assertEquals("This is mocked!", returnedFoo);
    }
}

1 Comment

Thanks for the suggestion, modified my post to aid in clarity. It appears that this response answers a question a tad different from what I was asking, sorry about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.