模拟的想法是提供模拟断开的连接。
@RunWith(PowerMockRunner.class)
@PrepareForTest(DataSource.class)
@PowerMockIgnore("javax.net.ssl.*")
public class ClientTestIT {
@Mock
HikariDataSource hikariDataSourceMock;
private void changeConnectionState(boolean isConnected) throws SQLException {
if(!isConnected) {
when(hikariDataSourceMock.getConnection()).thenThrow(SQLException.class);
} else {
when(hikariDataSourceMock.getConnection()).thenCallRealMethod();
}
}
错误:
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
[main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
[main] INFO service.TimestampService - Deleting all timestamps
[main] INFO service.TimestampService - Finding all timestamps
[main] INFO com.zaxxer.hikari.HikariDataSource - null - Starting...
[main] ERROR com.zaxxer.hikari.pool.HikariPool - null - Error thrown while acquiring connection from data source
java.lang.NullPointerException
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:353)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473)
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:562)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
at com.zaxxer.hikari.HikariDataSource$MockitoMock$1329021067.getConnection$accessor$1lRhyg9u(Unknown Source)
at com.zaxxer.hikari.HikariDataSource$MockitoMock$1329021067$auxiliary$eQlaVIuf.call(Unknown Source)
at org.mockito.internal.creation.bytebuddy.InterceptedInvocation$SuperMethod$FromCallable.invoke(InterceptedInvocation.java:195)
at org.mockito.internal.creation.bytebuddy.InterceptedInvocation.callRealMethod(InterceptedInvocation.java:131)
at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:43)
at org.mockito.internal.stubbing.StubbedInvocationMatcher.answer(StubbedInvocationMatcher.java:35)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:95)
at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:32)
at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:36)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:57)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:43)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptSuperCallable(MockMethodInterceptor.java:119)
at com.zaxxer.hikari.HikariDataSource$MockitoMock$1329021067.getConnection(Unknown Source)
at ClientTestIT.changeConnectionState(ClientTestIT.java:143)
at ClientTestIT.testWhenInsertingBatchAndConnectionMockedAndDisabled_ReturnFailedResultFromService(ClientTestIT.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
at org.powermock.modules.junit4.legacy.internal.impl.testcaseworkaround.PowerMockJUnit4LegacyTestMethodRunner.runUnprotected(PowerMockJUnit4LegacyTestMethodRunner.java:103)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1846)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:810)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:744)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:442)
at org.powermock.modules.junit4.legacy.internal.impl.testcaseworkaround.PowerMockJUnit4LegacyTestMethodRunner.run(PowerMockJUnit4LegacyTestMethodRunner.java:66)
at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
at org.powermock.modules.junit4.legacy.internal.impl.testcaseworkaround.PowerMockJUnit4LegacyTestClassMethodsRunner.run(PowerMockJUnit4LegacyTestClassMethodsRunner.java:93)
at org.powermock.modules.junit4.legacy.internal.impl.PowerMockJUnit4LegacyRunnerDelegateImpl$1.runUnprotected(PowerMockJUnit4LegacyRunnerDelegateImpl.java:69)
at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
at org.powermock.modules.junit4.legacy.internal.impl.PowerMockJUnit4LegacyRunnerDelegateImpl.run(PowerMockJUnit4LegacyRunnerDelegateImpl.java:85)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.junit.runner.JUnitCore.run(JUnitCore.java:130)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
[main] ERROR com.zaxxer.hikari.pool.HikariPool - null - Exception during pool initialization.
如何正确模拟它?