我正在为以下方法编写 TestCase
@Repository
public class CustomerContactDaoImpl implements CustomerContactDao {
@Autowired
@Qualifier(value = "dcv_jdbc_template")
private JdbcTemplate jdbcTemplate;
@Override
public void insertCustomer(Contact contact) {
SimpleJdbcCall call = new SimpleJdbcCall(jdbcTemplate).withProcedureName("ins_customerinfo");
try {
call.execute(new MapSqlParameterSource().addValue("customerid", contact.getCustomerID())));
} catch (Exception e) {}
}
这是我的测试用例
@RunWith(PowerMockRunner.class)
public class CustomerContactDaoImplTest {
@InjectMocks
CustomerContactDaoImpl customerContactDaoImpl;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void insertCustomerContactTest() throws Exception {
Contact contact = Mockito.mock(Contact.class);
JdbcTemplate jdbcTemplateMock = Mockito.mock(JdbcTemplate.class);
SimpleJdbcCall mockedSimpleJdbcCall = Mockito.mock(SimpleJdbcCall.class);
PowerMockito.whenNew(SimpleJdbcCall.class).withArguments(jdbcTemplateMock).thenReturn(mockedSimpleJdbcCall);
mockedSimpleJdbcCall.execute(new MapSqlParameterSource().addValue("customerid", contact.getCustomerID())));
customerContactDaoImpl.insertCustomerContact(contact);
}
}
我收到错误
模拟 JDBCTemplate 时,出现 No DataSource Specified 错误
请让我知道如何解决此错误
org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're referring to.
Matching constructors in class org.springframework.jdbc.core.simple.SimpleJdbcCall were:
org.springframework.jdbc.core.simple.SimpleJdbcCall( javax.sql.DataSource.class )
org.springframework.jdbc.core.simple.SimpleJdbcCall( org.springframework.jdbc.core.JdbcTemplate.class )
at org.powermock.reflect.internal.WhiteboxImpl.throwExceptionWhenMultipleConstructorMatchesFound(WhiteboxImpl.java:1723)
at org.powermock.reflect.internal.WhiteboxImpl.findUniqueConstructorOrThrowException(WhiteboxImpl.java:1097)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubstituteMock(DefaultConstructorExpectationSetup.java:94)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withArguments(DefaultConstructorExpectationSetup.java:54)
at com.nrg.bccd.dao.test.CustomerContactDaoImplTest.insertCustomerContactTest(CustomerContactDaoImplTest.java:71)
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.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)