1

I am in the process of creating test cases for a project. How the project is designed, it is difficult to go ahead and do simple unit tests and seems to be missing a dependency in order to use Mockito for mocking functions and objects. Here is what I have:

@RunWith(SpringRunner.class)
public class RequestHandlerTest {
    private byte[] buffer;
    private Connection conn;
    private Server server;

    @Mock ResponseHandler response;
    @Mock Services serviceImpl;
    @InjectMocks RequestHandler request;

    @Before
    // This just creates the buffer in order to simulate sending and 
    // receiving a response across a server and client
    public void createRequests(); 

    @After
    public void cleanUpBuffer(); // Cleans up buffer after test

    @Test
    public void TestReceiveMessage() {
        doNothing().when(response.updataCache(conn, reqID));
        ... // Do nothing for service function
        handler.receiveMsg(conn, buffer);
        // Verify request
    }
}

Now whenever I run the test I am getting the following exception:

 org.mockito.exceptions.base.MockitoException: At the moment you cannot provide own implementations of MockHandler.
Please see the javadocs for the MockMaker interface.
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.initMocks(MockitoTestExecutionListener.java:68)
    at org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.prepareTestInstance(MockitoTestExecutionListener.java:53)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at 

    org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

I am not sure what is causing this and what needs to be done to resolve this. I tried setting initializing Mockito, changing @Mock to @MockBean with @Autowired, and also tried just setting my values using a wrapper class. Is there something I could be missing? I have never seen this before.

3 Answers3

2

First of all, you don't need to use SpringRunner here. Replace @RunWith(SpringRunner.class) with @RunWith(MockitoJUnitRunner.class)

Secondly, if this problem still appears, try to use next (assuming that RequestHandlerImpl is the implementation of RequestHandler):

@InjectMocks RequestHandler request = new RequestHandlerImpl();

or even @InjectMocks RequestHandlerImpl request

amseager
  • 5,795
  • 4
  • 24
  • 47
  • The issue is still persisting, but the stack trace is now much shorter. This does work with other tests, For some reason, it is failing as soon as I try to mock an object. I may need to look at the design and see if there is a better way to implement what is there to make it easier to test and maintain. – Brandon Manning Mar 06 '19 at 13:49
  • Maybe you could also post RequestHandler, ResponseHandler, Services source code? – amseager Mar 06 '19 at 15:10
  • Unfortunately I'm not able to due to policy :/ I know the issue occurs once mock is called from stepping through the debugger. When I do mock the classes on my own I am to do what I want, but it would be far more time consuming. I am going to keep looking into it to see what is causing it. – Brandon Manning Mar 06 '19 at 16:41
  • Part of the solution you gave did help, I was just missing configuration for some dependencies. – Brandon Manning Mar 14 '19 at 16:53
0

Ok so after looking into this, I saw that there were a lot of beans that were being Autowired. So I needed to add the context configuration for the classes I have dependencies on. Then I am able to use the annotations for Mock and Inject Mock. So my fixture becomes something like this:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {RequestHandler.class, ResponseHandler.class,
      DataCache.class, Service.class})
public class RequestHandlerTest {
   @Mock private ResponseHandler response;
   @Mock private Service serv;

   @InjectMocks
   @Resource
   private RequestHandler request;

   public void setUp() {
      MockitoAnnotations.initMocks(this);

      // Add mock data logic using doAnswer or doNothing for void methods if needed 
      // and adding when().then() for mocking return values for other functions

   }

   @Test
   public void makeRequest() {
      assertTrue(request.sendRequest(Object message));
   }
}

Pulled information from Mocking Void Methods with Mockito and from an answer in this question here.

  • @amseager's answer is still correct. You either create a unit test, with mocks and run by a`MockitoJUnitRunner` (which will initialize and inject mocks), or an integration test run by `SpringRunner` (which will create a spring context with proper beans being injected). – daniu Mar 14 '19 at 17:11
  • Yes when creating the test it is correct. But if the `ContextConfiguration` is not there, it fails to even create a mock. That is why I was getting the exception. Once the annotation is removed it fails. The designed is tightly coupled and we are having issues with creating our mocks because of it :( – Brandon Manning Mar 14 '19 at 17:43
0

This is resolved in mockito2 2.x.x version.

org.powermock powermock-api-mockito2 2.0.9 test

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 10 '22 at 19:29