일부 객체에서 콩 주입에 SpringBeanAutowiringSupport을 사용합니다. 문제는 콩의 주입이 jUnit 테스트에서 작동하지 않는다는 것입니다. 테스트 용으로 SpringJUnit4ClassRunner가 사용됩니다.SpringBeanAutowiringSupport가 jUnit 테스트에서 빈을 삽입하지 않습니다.
public class DossierReportItemXlsImporterImpl implements DossierRerportItemXlsImporer {
private final Logger logger = Logger.getLogger(getClass());
// are not autowired.
@Autowired
private DossierReportService dossierReportService;
@Autowired
private DossierReportItemService dossierReportItemService;
@Autowired
private NandoCodeService nandoCodeService;
public DossierReportItemXlsImporterImpl(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
//...
}
public class DossierRerportItemXlsImporerTest extends AuditorServiceTest{
// injected OK
@Autowired
private DossierReportService dossierReportService;
@Autowired
private DossierReportItemService dossierReportItemService;
@Test
public void testXlsImport(){
DossierRerportItemXlsImporer importer = new DossierReportItemXlsImporterImpl();
importer.processImport(createDossierReport(), loadFile());
// ...
}
// ...
}
사람이 어떤 생각을 가지고 있는가, 왜 JUnit 테스트에서 작동하지 않습니다 SpringBeanAutowiringSupport
를 사용하여 주입?
테스트 러너는 컨텍스트를로드하기 위해'ContextLoader'를 사용하지 않기 때문에주의하십시오. 이것은'SpringBeanAutowiringSupport'에 의해 사용됩니다. 기본적으로 컨텍스트를 감지하지 못합니다. 추가 된 어려움으로 정규'ApplicationContext' 대신에'WebApplicationContext'가 될 것으로 기대합니다. 이 문제를 회피하기 위해서는'ApplicationContext'를 삽입하고'getAutowireCapableBeanFactory(). autowireBean (yourInstance); '를 호출하여 수동으로 배선 작업을 수행 할 수 있습니다. –