Issue
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.
Solution
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
Answered By - amseager
Answer Checked By - David Marino (JavaFixing Volunteer)