2011-01-14 11 views

risposta

12

Foo non è un fagiolo gestito, lo stai istanziando tu stesso. Quindi Spring non ha intenzione di autorizzare nessuna delle sue dipendenze per te.

+2

eh. oh uomo, ho bisogno di dormire. è così ovvio. Grazie! – Upgradingdave

7

Stai creando una nuova istanza di Foo. Quell'istanza non ha idea del contenitore di iniezione di dipendenza Spring. Devi eseguire l'autowire foo nel tuo test:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"beans.xml"}) 
public class BarTest { 

    @Autowired 
    // By the way, the by type autowire won't work properly here if you have 
    // more instances of one type. If you named them in your Spring 
    // configuration use @Resource instead 
    @Resource(name = "mybarobject") 
    Object bar; 
    @Autowired 
    Foo foo; 

    @Test 
    public void testBar() throws Exception { 
      //this works 
     assertEquals("expected", bar.someMethod()); 
      //this doesn't work, because the bar object inside foo isn't autowired? 
     assertEquals("expected", foo.someMethodThatUsesBar()); 
    } 
} 
+0

ha perfettamente senso, grazie mille! – Upgradingdave