복붙노트

[SPRING] SpringBeanAutowiringSupport가 jUnit 테스트에서 빈을 삽입하지 않습니다.

SPRING

SpringBeanAutowiringSupport가 jUnit 테스트에서 빈을 삽입하지 않습니다.

일부 객체에서 bean 주입을 위해 SpringBeanAutowiringSupport를 사용합니다. 문제는 콩의 주입이 jUnit 테스트에서 작동하지 않는다는 것입니다. 테스트 용으로 SpringJUnit4ClassRunner가 사용됩니다.

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());
        // ...
    }
  // ...
}

누구든지 SpringBeanAutowiringSupport를 사용하는 주입이 jUnit 테스트에서 작동하지 않는 이유를 알고 있습니까?

해결법

  1. ==============================

    1.잘 봄 + junit 팀은 이미 이것을 고쳤습니다. 이 링크를보세요 -> 스프링 유닛 테스트

    잘 봄 + junit 팀은 이미 이것을 고쳤습니다. 이 링크를보세요 -> 스프링 유닛 테스트

    그렇지 않으면 스프링 컨텍스트를 호출하고 getBean 메소드를 사용할 수 있습니다. 그런 식으로 junit 테스트 대신 클래스 내부의 간단한 기본 테스트로 수행 할 수도 있습니다

    ** 참고로 spring + junit 설정을 사용한다면 test-spring-context.xml을 테스트 패키지에 넣어야한다.

  2. ==============================

    2.MY 덕분입니다. 데 니움, 그의 해결책이 작동합니다.

    MY 덕분입니다. 데 니움, 그의 해결책이 작동합니다.

    public class DossierReportItemXlsImporterImpl implements DossierRerportItemXlsImporer {
    
        private final Logger logger = Logger.getLogger(getClass());
    
        @Autowired
        private DossierReportService dossierReportService;
        @Autowired
        private DossierReportItemService dossierReportItemService;
        @Autowired
        private NandoCodeService nandoCodeService;
    
        public DossierReportItemXlsImporterImpl(final ApplicationContext contex){
            contex.getAutowireCapableBeanFactory().autowireBean(this);
        }
    
        //...
    }
    
    
     public class DossierRerportItemXlsImporerTest extends AuditorServiceTest{
    
            @Autowired
            private ApplicationContext context;
            @Autowired
            private DossierReportService dossierReportService;
            @Autowired
            private DossierReportItemService dossierReportItemService;
    
            @Test
            public void testXlsImport(){
                DossierRerportItemXlsImporer importer = new DossierReportItemXlsImporterImpl(context);
                importer.processImport(createDossierReport(), loadFile());
                // ...
            }
          // ...
        }
    
  3. from https://stackoverflow.com/questions/25922956/springbeanautowiringsupport-does-not-inject-beans-in-junit-tests by cc-by-sa and MIT license