带弹簧的单元测试服务层

时间:2016-07-05 13:47:08

标签: spring unit-testing service-layer

我试图在我的网络应用程序的服务层中对此类进行单元测试:

@Service
public class MyServiceImpl implements MyService{

    @Autowired
    private MyDao myDao;
    @Autowired
    private MapperFacade mapper;

    @Log
    private Logger logger;

    @Override
    public CriteriaResults<MasterSlaveCurrenciesDto> someMethod(CriteriaParameters parameters) {}
}

使用此类注入Logger:

@Component
public class LoggerInjector implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
        ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
            @Override
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(field);
                if(field.getAnnotation(Log.class) != null) {
                    Logger logger = LoggerFactory.getLogger(bean.getClass());
                    field.set(bean, logger);
                }
            }
        });
        return bean;
    }
}

我使用Mockito来模拟DAO层,但我不知道如何为其他2个依赖项做同样的事情:

  • Orika mapper:MapperFacade mapper
  • 记录器

感谢您的帮助。

0 个答案:

没有答案