我正在为项目创建测试用例。该项目的设计方式,很难继续进行简单的单元测试,并且似乎缺少依赖性,以便将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)
我不确定是什么原因引起的,以及需要做什么才能解决此问题。我尝试设置初始化的oikito,并使用@Autowired将@mock更改为@mockbean,并尝试使用包装类别设置我的值。我可能会缺少一些东西吗?我以前从未见过。
首先,您无需在此处使用SpringRunner
。用@RunWith(MockitoJUnitRunner.class)
@RunWith(SpringRunner.class)
其次,如果仍然出现此问题,请尝试使用下一步(假设请求Handlerimpl是请求Handler的实现):
@InjectMocks RequestHandler request = new RequestHandlerImpl();
甚至@InjectMocks RequestHandlerImpl request
好的,所以在研究了这一点之后,我看到有很多自动化的豆子。因此,我需要为我有依赖性的类添加上下文配置。然后,我能够使用注释进行模拟和注入模拟。所以我的固定装置变成了这样的东西:
@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));
}
}
从嘲笑空隙方法中汲取信息,并从此处的这个问题中的答案中获取信息。
这是在Mockito2 2.x.x版本中解决的。
org.powermock powermock-api-mockito2 2.0.9测试