我正在为一个项目创建测试用例。如何设计项目,很难进行简单的单元测试,并且似乎缺少依赖项,因此无法使用Mockito来模拟函数和对象。这是我所拥有的:
@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
}
}
现在,每当我运行测试时,都会收到以下异常:
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)
我不确定是什么原因导致的,以及解决该问题需要采取的措施。我尝试设置初始化Mockito,使用@Autowired将@Mock更改为@MockBean,还尝试使用包装器类设置我的值。有什么我可能会想念的吗?我以前从未见过。
答案 0 :(得分:1)
首先,您无需在此处使用SpringRunner
。将@RunWith(SpringRunner.class)
替换为@RunWith(MockitoJUnitRunner.class)
第二,如果仍然出现此问题,请尝试使用下一个(假设RequestHandlerImpl是RequestHandler的实现):
@InjectMocks RequestHandler request = new RequestHandlerImpl();
甚至@InjectMocks RequestHandlerImpl request
答案 1 :(得分:0)
好吧,在研究了这一点之后,我看到有很多正在自动接线的bean。因此,我需要为我所依赖的类添加上下文配置。然后,我可以对Mock和Inject Mock使用注释。这样我的装置就变成这样:
@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));
}
}
从Mocking Void Methods with Mockito和问题here的答案中提取信息。