2017-02-20 2 views
0

ich eine Test-Klasse geschrieben.Autowired Injektion fehlgeschlagen

@ActiveProfiles("dev") 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {  "classpath:spring/appContext.xml","classpath:spring/appContext-datasource.xml"}) 
@Transactional 
public class ReadReconciliationFileTest { 
@Autowired 
private OriginalReconciliationInformationExtMapper originalReconciliationInformationExtMapper; 
@Autowired 
private ProblematicReconciliationInformationMapper problematicReconciliationInformationMapper; 
@Test 
public void insertReconciliationInformation(){ 
    ReadReconciliationFileFactory readFactory=new AllinReadReconciliationFileFactory(); 
    ReadReconciliationFile read=readFactory.produce(); 
    List<ReconciliationBean> list=read.readFile("/Users/wuchangming/Documents/work/通联/tlt_javademo/PDS2006040000004450420170216.txt"); 
    for(int i=0;list!=null&&i<list.size();i++){ 
     originalReconciliationInformationExtMapper.insertInformation(list.get(i)); 
    } 
    List<ReconciliationBean> list2=originalReconciliationInformationExtMapper.getInformationList(null); 
    List<String> list3=problematicReconciliationInformationMapper.getProblematicDataList("9",null); 
    Assert.assertEquals(list.size(),list2.size()+list3.size()); 
} 
} 

In dieser Klasse Autowired gültig ist. Aber bei der Umsetzung der ReadReconciliationFile Klasse AllinReadReconciliationFileImpl ist Autowired Injektion null. enter image description here

bitte helfen mich. danke!

+0

Zeigen Sie Ihren gesamten Code? Normalerweise passiert dies, wenn Sie versehentlich Ihre Testklasse in Ihrer Testklasse "neu" machen. – Tobb

+0

Nichts konnte nicht automatisch verdrahten ... Sie erstellen eine neue Instanz in der Testmethode selbst ... Bei Verwendung der automatischen Verdrahtung kann eine '@ Autowired'-Eigenschaft nicht 'null' sein (es sei denn, Sie sagen es, dass es möglich ist). Wenn die automatische Verdrahtung fehlschlägt, wird Ihre Anwendung beim Start einfach hochgefahren. –

Antwort

0

Diese Linien:

ReadReconciliationFileFactory readFactory=new AllinReadReconciliationFileFactory(); 
ReadReconciliationFile read=readFactory.produce(); 

Will (wahrscheinlich) erstellen AllinReadReconciliationFileImpl dass Spring weiß nichts, und damit nichts autowire kann.

Sie müssen @Inject das Objekt dieser Klasse, und verwenden Sie es.

+0

danke! Benötige ich ein Objekt von AllinReadReconciliationFileFactory? –

+0

Sie benötigen 'AllinReadReconciationFileImpl', das von Spring erstellt wird. Normalerweise benutzt man entweder eine Fabrik oder einen Spring, beides ergibt wenig Sinn. – Tobb

+0

Vielen Dank Ihre Antwort ist sehr hilfreich für mich –

Verwandte Themen