我需要为下面的类方法编写单元测试用例。 eRecipeResultService.getERecipeSendAndInvalidateResultForRecipeIds(idList, null);
。我创建了类并尝试注入它。但是当运行测试类时,它给出了这样的错误。
但仍然相同。首先我的尝试,在ERCipeResultTest类中创建对象,并将模拟对象传递给它们。然后我在运行单元测试时,' ERecipeInvalidationResultDAO'这个类开始为空。
第二次尝试移动时,对象创建部分为xml。但现在给出了错误,刚刚开始跑步。
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eRecipeResultServiceImpl' defined in class path resource [se/cambio/cosmic/eprescription/offline-ERecipeResultService-context.xml]: Unsatisfied dependency expressed through constructor parameter 0: Could not convert argument value of type [java.lang.String] to required type [se.cambio.cosmic.eprescription.dto.spi.ERecipeResultEntityDTOMapper]: Failed to convert value of type [java.lang.String] to required type [se.cambio.cosmic.eprescription.dto.spi.ERecipeResultEntityDTOMapper]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [se.cambio.cosmic.eprescription.dto.spi.ERecipeResultEntityDTOMapper]: no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
我的班级:
public class ERecipeResultServiceImpl<D extends se.cambio.cosmic.eprescription.dto.ERecipeResultEntityDTO, MD extends se.cambio.cosmic.eprescription.dto.ERecipeResultMutableEntityDTO>
extends se.cambio.cosmic.eprescription.service.impl.AbstractERecipeResultService<D, MD>
implements se.cambio.cosmic.eprescription.service.ERecipeResultService<D, MD>
{
@Resource
private ServiceCommonUtil serviceCommonUtil;
@Resource
private ERecipeResultLoadingCriteriaMapper loadingCriteriaMapper;
@Resource
private ERecipeInvalidationResultDAO invalidationResultDAO;
@Resource
private ERecipeSendAndInvalidateResultDTOInternalFactory invalidateResultDTOFactory;
@Resource
private ERecipeInvalidationResultDTOMapper eRecipeInvalidationResultDTOMapper;
@Resource
private ERecipeInvalidationResultAuditAccessor eRecipeInvalidationResultAuditAccessor;
@Resource
private SessionFactory sessionFactory;
public ERecipeResultServiceImpl(final ERecipeResultEntityDTOMapper<D> mapper,
final ERecipeResultMutableEntityDTOMapper<MD> mutableMapper,
final ERecipeResultDAO dao)
{
super(mapper, mutableMapper, dao);
}
@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public List<ERecipeSendAndInvalidateResultDTO> getERecipeSendAndInvalidateResultForRecipeIds(final List<ItemDistributionEntityDTO.Id> ids,
final DateTime dateTime)
{
final List<String> stringIds = getStringIds(ids);
final List<ERecipeInvalidationResult> results =
invalidationResultDAO.getERecipeInvalidationResultForRecipeIds(stringIds);
List<ERecipeInvalidationResult> newList = new ArrayList<ERecipeInvalidationResult>();
if (dateTime != null)
{
for (final ERecipeInvalidationResult result : results)
{
final ERecipeInvalidationResult revisionOnDate = findRevisionOnDate(result.getId(), dateTime);
if (revisionOnDate != null)
{
newList.add(revisionOnDate);
}
}
}
else
{
newList = results;
}
final ImmutableMap<String, ERecipeInvalidationResult> map =
Maps.uniqueIndex(newList, new Function<ERecipeInvalidationResult, String>()
{
@Override
public String apply(final ERecipeInvalidationResult input)
{
return input.getRecipes().iterator().next().getRecipeID();
}
});
return FluentIterable.from(ids).transform(getInvalidationResultMapperFunction(map, dateTime)).toList();
}
public ERecipeInvalidationResult findRevisionOnDate(final ERecipeInvalidationResult.Id id,
final DateTime baseLine)
{
final List<? extends Serializable> revisions = eRecipeInvalidationResultAuditAccessor.getRevisions(id);
final long inMills = baseLine.withSecondOfMinute(59).withMillisOfSecond(999).getMillis(); //Find up to last second of the minute
for (int i = revisions.size() - 1; i >= 0; i--)
{
final Number revisionId = (Number) revisions.get(i);
final long revisionDate =
AuditReaderFactory.get(sessionFactory.getCurrentSession()).getRevisionDate(revisionId).getTime();
if (((revisionDate <= inMills)))
{
return eRecipeInvalidationResultAuditAccessor.findRevision(id, revisionId);
}
}
return null;
}
private Function<ItemDistributionEntityDTO.Id, ERecipeSendAndInvalidateResultDTO> getInvalidationResultMapperFunction(final ImmutableMap<String, ERecipeInvalidationResult> map,
final DateTime dateTime)
{
return new Function<ItemDistributionEntityDTO.Id, ERecipeSendAndInvalidateResultDTO>()
{
@Override
public ERecipeSendAndInvalidateResultDTO apply(final ItemDistributionEntityDTO.Id id)
{
final D convert = getMapper().convert(getDAO().getERecipeResultForRecipeId(id.toString()));
final List<ERecipeInvalidationResult> invalidationResult = getMatchERecipeInvalidationResult(map, id);
return invalidateResultDTOFactory.newDTO((ERecipeResultViewDTO) convert,
eRecipeInvalidationResultDTOMapper.convert(Lists.newArrayList(invalidationResult)),
id.toString(),
getInvalidationResultResponseCode(Lists.newArrayList(invalidationResult)));
}
};
}
private List<ERecipeInvalidationResult> getMatchERecipeInvalidationResult(final ImmutableMap<String, ERecipeInvalidationResult> map,
final ItemDistributionEntityDTO.Id id)
{
final ERecipeInvalidationResult eRecipeInvalidationResult = map.get(id.toString());
if (Checks.checkNotNull(eRecipeInvalidationResult))
{
return Lists.newArrayList(eRecipeInvalidationResult);
}
else
{
return new ArrayList<ERecipeInvalidationResult>();
}
}
private List<String> getStringIds(final List<ItemDistributionEntityDTO.Id> ids)
{
final Function<ItemDistributionEntityDTO.Id, String> func =
new Function<ItemDistributionEntityDTO.Id, String>()
{
@Override
public String apply(final ItemDistributionEntityDTO.Id input)
{
return input.toString();
}
};
return FluentIterable.from(ids).transform(func).toList();
}
/**
* return recipe status and invalidation status for giving recipe ID.
*
* @param recipeId
*
* @return ERecipeSendAndInvalidateResultDTO
*/
@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public ERecipeSendAndInvalidateResultDTO getERecipeSendAndInvalidateResultForRecipeId(final ItemDistributionEntityDTO.Id recipeId)
{
final D convert =
getMapper().convert(getDAO().getERecipeResultForRecipeId(serviceCommonUtil.getIdStringValue(recipeId)));
final List<ERecipeInvalidationResult> invalidationResult =
invalidationResultDAO.getERecipeInvalidationResultForRecipeId(serviceCommonUtil.getIdStringValue(recipeId));
return invalidateResultDTOFactory.newDTO((ERecipeResultViewDTO) convert,
eRecipeInvalidationResultDTOMapper.convert(invalidationResult),
recipeId.toString(),
getInvalidationResultResponseCode(invalidationResult));
}
private EPrescriptionStatus getInvalidationResultResponseCode(final List<ERecipeInvalidationResult> invalidationResult)
{
EPrescriptionStatus code = null;
if (!invalidationResult.isEmpty())
{
code = isPartiallyInvalidated(invalidationResult) ? EPrescriptionStatus.ERECIPE_PARTLY_INVALIDATED : null;
}
return code;
}
// If patient has used part of the recipe then we shall only invalidate the e-recipe, not the cosmic recipe.
// When user invalidates a recipe that is "used" then we get a "partially invalidated" message back from pharmacy
// and cosmic recipe is not invalidated.
private boolean isPartiallyInvalidated(final List<ERecipeInvalidationResult> invalidationResult)
{
return Iterables.all(invalidationResult, new Predicate<ERecipeInvalidationResult>()
{
@Override
public boolean apply(final ERecipeInvalidationResult input)
{
return Iterables.any(input.getResponse(),
new Predicate<ERecipeInvalidationResultResponse>()
{
@Override
public boolean apply(final ERecipeInvalidationResultResponse input)
{
// TODO use constants for the codes
return input.getCode().equals("3");
}
});
}
});
}
@Override
public D getERecipeResultForRecipeId(final ItemDistributionEntityDTO.Id recipeId)
{
return getMapper().convert(getDAO().getERecipeResultForRecipeId(serviceCommonUtil.getIdStringValue(recipeId)));
}
@Override
public List<D> getERecipeResults(final ERecipeResultLoadingCriteriaDTO eRecipeResultCriteria)
{
return getMapper().convert(getDAO().getERecipeResults(loadingCriteriaMapper.mapERecipeResultLoadingCriteria(eRecipeResultCriteria)));
}
@Override
public D getERecipeResultForRecipeUUID(final String recipeUUID)
{
return getMapper().convert(getDAO().getERecipeResultForRecipeUUID(recipeUUID));
}
}
我的bean配置.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="se.cambio.cosmic.eprescription.persistence.ERecipeInvalidationResultDAO" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="se.cambio.cosmic.eprescription.persistence.ERecipeInvalidationResultDAO"/>
</bean>
<bean id= "eRecipeInvalidationResultAuditAccessor" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="se.cambio.cosmic.eprescription.ERecipeInvalidationResultAuditAccessor"/>
</bean>
<bean id= "sessionFactory" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.hibernate.SessionFactory"/>
</bean>
<bean id="eRecipeResultEntityDTOMapper" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="se.cambio.cosmic.eprescription.dto.spi.ERecipeResultEntityDTOMapper"/>
</bean>
<bean id="eRecipeResultMutableEntityDTOMapper" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="se.cambio.cosmic.eprescription.dto.spi.ERecipeResultMutableEntityDTOMapper"/>
</bean>
<bean id="eRecipeResultDAO" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="se.cambio.cosmic.eprescription.persistence.ERecipeResultDAO"/>
</bean>
<bean id="eRecipeResultServiceImpl" class="se.cambio.cosmic.eprescription.service.impl.ERecipeResultServiceImpl" >
<constructor-arg value="eRecipeResultEntityDTOMapper" type="se.cambio.cosmic.eprescription.dto.spi.ERecipeResultEntityDTOMapper" index="0" />
<constructor-arg value="eRecipeResultMutableEntityDTOMapper" type="se.cambio.cosmic.eprescription.dto.spi.ERecipeResultMutableEntityDTOMapper" index="1"/>
<constructor-arg value="eRecipeResultDAO" type="se.cambio.cosmic.eprescription.persistence.ERecipeResultDAO" index="2"/>
</bean>
<!-- <import resource="offline-test-context.xml"/>-->
</beans>
需要一些专家帮助才能解决此问题。