父级:
@Service
public class ProviderBasicDAO {
@Autowired
protected JdbcTemplate jdbcTemplate;
public List<Long> getNetworkIds(List<EHIPlan> ehiPlans) {
if (CollectionUtils.isEmpty(ehiPlans)) {
return new ArrayList<>();
}
return jdbcTemplate.execute(buildSQL(ehiPlans), new CallableStatementCallback<List<Long>>() {
@Override
public List<Long> doInCallableStatement(CallableStatement callableStatement)
throws SQLException, DataAccessException {
ResultSet resultSet = callableStatement.executeQuery();
List<Long> networkIds = new ArrayList<>();
while (resultSet.next()) {
networkIds.add(resultSet.getLong("network_id"));
}
return networkIds;
}
});
}
}
儿子课程:
public class ProviderNameDAO extends ProviderBasicDAO {
@Autowired
private JdbcTemplate jdbcTemplate;
@Value("#[provider.log.to]")
private int logTo;
private int apiType = 2;
public List<Provider> getProvider(ZipRepository coordinate, NameSearchRequest request, List<Long> carrierIds,
List<EHIPlan> ehiPlans) throws SQLException, ClassNotFoundException {
List<Long> networkIds = getNetworkIds(ehiPlans);
if (!CollectionUtils.isEmpty(ehiPlans) && CollectionUtils.isEmpty(networkIds)) {
return new ArrayList<>();
}
return jdbcTemplate.execute(new CallableStatementCreator() {
@Override
public CallableStatement createCallableStatement(Connection connection) throws SQLException {
final StringBuffer sqlBuffer = new StringBuffer(
"{call pv_mc_infosearch_by_name(?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,? ,?)}");
CallableStatement preparedStatement = connection.prepareCall(sqlBuffer.toString());
preparedStatement.setInt(1, apiType);
preparedStatement.setString(2, SecurityUtil.escapeSQLChar(request.getFirstName()));
preparedStatement.setString(3, SecurityUtil.escapeSQLChar(request.getLastName()));
preparedStatement.setString(4, SecurityUtil.escapeSQLChar(request.getFacilityName()));
preparedStatement.setDouble(5, coordinate.getLatitude());
preparedStatement.setDouble(6, coordinate.getLongitude());
preparedStatement.setInt(7, request.getRadius());
preparedStatement.setString(8, StringUtils.join(networkIds,","));
preparedStatement.setString(9, StandardizerUtils.convertToString(carrierIds));
preparedStatement.setInt(10, SearchType.getTypeValue(request.getSearchType()));
preparedStatement.setInt(11, logTo);
preparedStatement.setString(12, UUID.randomUUID().toString());
return preparedStatement;
}
}, new CallableStatementCallback<List<Provider>>() {
public List<Provider> doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
Map<String, Provider> idAndProviderDetailsMap = new HashMap<>();
boolean isResultSet = cs.execute();
int rsCount = 0;
while (isResultSet) {
ResultSet childRs = cs.getResultSet();
if (rsCount == 0) {
parseProvider(childRs,idAndProviderDetailsMap);
}
if (rsCount == 1) {
parseAddress(childRs,idAndProviderDetailsMap);
}
if (rsCount == 2) {
parseSpecialty(childRs,idAndProviderDetailsMap);
}
if (rsCount == 3) {
parseAffiliation(childRs,idAndProviderDetailsMap);
}
if (rsCount == 4) {
parsePhone(idAndProviderDetailsMap, childRs);
}
if (rsCount == 5) {
parseCarrierGroupIdMap(childRs, idAndProviderDetailsMap);
}
if (rsCount == 6) {
parseLanguage(childRs,idAndProviderDetailsMap);
}
childRs.close();
isResultSet = cs.getMoreResults();
rsCount++;
}
cs.close();
return new ArrayList<>(idAndProviderDetailsMap.values());
}
});
}
1. **my test code:**
@RunWith(PowerMockRunner.class)
@PrepareForTest({StandardizerUtils.class,StringUtils.class,UUID.class})
public class ProviderNameDAOTest {
ProviderNameDAO providerNameDAO;
ProviderBasicDAO providerBasicDAO;
NamedParameterJdbcTemplate namedParameterJdbcTemplate;
JdbcTemplate jdbcTemplate;
@Before
public void preTest() {
providerNameDAO=new ProviderNameDAO();
providerBasicDAO=new ProviderBasicDAO();
jdbcTemplate=PowerMockito.mock(JdbcTemplate.class);
Whitebox.setInternalState(providerNameDAO, "jdbcTemplate", jdbcTemplate);
providerBasicDAO.jdbcTemplate=jdbcTemplate;
}
@Test
public void testParseAffiliation() {
String name="hehe";
long key=1;
String value="hehe";
ZipRepository coordinate=new ZipRepository();
coordinate.setLongitude(20);
coordinate.setLatitude(10);
NameSearchRequest request=new NameSearchRequest();
request.setFirstName(name);
request.setLastName(name);
request.setFacilityName(name);
request.setRadius(20);
request.setSearchType("physician");
List<Long> carrierIds=new ArrayList<>();
carrierIds.add(key);
List<EHIPlan> ehiPlans=new ArrayList<>();
EHIPlan ehiPlan=new EHIPlan();
ehiPlan.setNetwork("haha");
ehiPlan.setPlanCategory("hehe");
ehiPlans.add(ehiPlan);
//networkIds
{
CallableStatement callableStatement=PowerMockito.mock(CallableStatement.class);
PowerMockito.when(jdbcTemplate.execute(Mockito.any(String.class),Mockito.any(CallableStatementCallback.class))).thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args=invocation.getArguments();
CallableStatementCallback arg = (CallableStatementCallback)args[1];
return ((CallableStatementCallback) arg).doInCallableStatement(callableStatement);
}
});
ResultSet resultSet =PowerMockito.mock(ResultSet.class);
try {
PowerMockito.when(callableStatement.executeQuery()).thenReturn(resultSet);
PowerMockito.when(resultSet.next()).thenReturn(true).thenReturn(false);
PowerMockito.when(resultSet.getLong(Mockito.any(String.class))).thenReturn(key);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//mian jdbc
{
//prarmeter 1
PowerMockito.mockStatic(StandardizerUtils.class);
PowerMockito.mockStatic(StringUtils.class);
PowerMockito.mockStatic(UUID.class);
Connection connection=PowerMockito.mock(Connection.class);
CallableStatement preparedStatement=PowerMockito.mock(CallableStatement.class);
PowerMockito.when(jdbcTemplate.execute(Mockito.any(CallableStatementCreator.class),Mockito.any(CallableStatementCallback.class)))
.thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args=invocation.getArguments();
CallableStatementCreator arg = (CallableStatementCreator)args[0];
return arg.createCallableStatement(connection);
}
});
try {
PowerMockito.when(connection.prepareCall(Mockito.any(String.class))).thenReturn(preparedStatement);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//prarmeter 2
CallableStatement cs=PowerMockito.mock(CallableStatement.class);
PowerMockito.when(jdbcTemplate.execute(Mockito.any(CallableStatementCreator.class),Mockito.any(CallableStatementCallback.class)))
.thenAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args=invocation.getArguments();
CallableStatementCallback arg = (CallableStatementCallback)args[1];
return arg.doInCallableStatement(cs);
}
});
try {
String y="N";
long l=2;
PowerMockito.when(cs.execute()).thenReturn(true);
ResultSet childRs=PowerMockito.mock(ResultSet.class);
PowerMockito.when(cs.getResultSet()).thenReturn(childRs);
PowerMockito.when(childRs.getString(Mockito.any(String.class))).thenReturn(y);
PowerMockito.when(childRs.getLong(Mockito.any(String.class))).thenReturn(l);
PowerMockito.when(cs.getMoreResults()).thenReturn(true).thenReturn(true).thenReturn(true)
.thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//test body
try {
List<Provider> result=providerNameDAO.getProvider(coordinate, request, carrierIds, ehiPlans);
Assert.assertEquals(1, result.size());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
**
1. error informations:
**
org.powermock.reflect.exceptions.MethodNotFoundException: No methods matching the name(s) executeQuery were found in the class hierarchy of class java.lang.Object.
at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1720)
at org.powermock.reflect.internal.WhiteboxImpl.getMethods(WhiteboxImpl.java:1745)
at org.powermock.reflect.internal.WhiteboxImpl.getBestMethodCandidate(WhiteboxImpl.java:983)
at org.powermock.core.MockGateway$MockInvocation.findMethodToInvoke(MockGateway.java:317)
at org.powermock.core.MockGateway$MockInvocation.init(MockGateway.java:356)
at org.powermock.core.MockGateway$MockInvocation.<init>(MockGateway.java:307)
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:142)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:125)
at com.ehi.provider.dao.ProviderNameDAOTest.testParseAffiliation(ProviderNameDAOTest.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
**
1. question:
**why the father class method not to uesed in son class? and meantime i am not sure my test code is right completely, if you find my error or have a better idea, please point out in here, thank you !!!