2013-02-24 11 views
5

Przeglądam niektóre samouczki, aby rozpocząć korzystanie z Spring Framework i natknąłem się na this one. Po przejściu przez to, natknąłem się na błędy poniżej. (I od tego czasu zaktualizowana używać wersji 3 ram wiosny, a ja wciąż się te same błędy.Wiosna 3 @Amianowane problemy z adnotacjami

Krótko mówiąc, jestem coraz NullPointerException gdzie wywołać metodę na klasy AutoWired.

moja klasa jest sayHello.

public class SayHello { 

private String name; 

public void setName(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

public void greet() { 
    System.out.println("Hello " + getName()); 
} 

} 

I wtedy ustawić następujące w /src/test/resources/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd"> 

<context:annotation-config/> 


    <bean id="hello" class="package.SayHello"> 
    <property name="name" value="John Smith" /> 
    </bean 

I wreszcie mam klasę AppTest, w której próbuję wykonać AutoWire obiekt SayHello do przetestowania.

public class AppTest { 

@Autowired 
private SayHello hello; 

@Test 
public void testApp() 
{ 
    hello.greet(); 
    Assert.assertTrue(true); 
} 
} 

Kiedy próbuję uruchomić AppTest pojawia się następujący błąd:

java.lang.NullPointerException 
at package.AppTest.testApp(AppTest.java:19) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

miałem spojrzeć na różnych artykułów i pytań na StackOverflow ale po wypróbowaniu kilku i nie nigdzie, myśli Stworzyłem nowy post.

Pozdrawiam.

Jeśli to nie pomaga, Herezje zależności, które używam (wiosna V3.2.1):

<dependencies> 

<!-- JUnit --> 
<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.11</version> 
    <scope>test</scope> 
</dependency> 


<!-- Spring --> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-beans</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context-support</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aop</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-jms</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>${org.springframework.version}</version> 
</dependency> 

Aktualizacja

Ja również próbowałem za pomocą @Autowired adnotację w głównym wniosku w następujący sposób. Nadal występuje błąd NullPointerException.

public class App 
{ 
    @Autowired 
    private static SayHello hello; 

    public static void main(String[] args) 
    { 
     hello.greet(); 
    } 
} 

Odpowiedz

6

Trzeba pewne adnotacje na AppTest:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:/applicationContext.xml") 

te adnotacje są zdefiniowane w wiosenno-testu, więc dodać tę zależność, a także:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-test</artifactId> 
    <version>${org.springframework.version}</version> 
    <scope>test</scope> 
</dependency> 
+0

Świetne, to prawda! Mój test przechodzi teraz i drukuje dane wyjściowe zgodnie z oczekiwaniami.Czy muszę zrobić coś podobnego, żeby zacząć pracować z główną aplikacją? Dzięki @zagyi! – cast01

+0

Nie, jeśli użyjesz klasy "App" zdefiniowanej w artykule, który łączysz, przetworzy ona adnotację zgodnie z instrukcją podaną w wierszu '' w konfiguracji xml. – zagyi

+0

Pozdrawiam @zagyi, nie byłem pewien, czy był sposób, w jaki mogłem to zrobić bez konieczności tworzenia instancji BeanFactory i jawnie za pomocą funkcji getBean(), id lubi używać adnotacji autowyred. – cast01

3

@Autowired adnotacja będzie działać tylko wtedy, gdy AppTest sama zarządza wiosny. Wygląda na to, że testujesz swoją konfigurację w prostym teście jednostkowym. To nie zadziała. Jeśli chcesz autowiring w testach JUnit, będziesz musiał popatrzeć na wsparcie testowania Spring. Ale to zupełnie inny temat.

+0

dziękuję @ chkal, Próbowałem również Autowired w głównej aplikacji i nadal otrzymuję ten sam błąd, źle zaktualizować pytanie, aby to pokazać. – cast01

1

W poradniku zostały połączone ten kod Tworzy komponent bean:

BeanFactory factory = new XmlBeanFactory(
    new ClassPathResource("application-context.xml")); 

SayHello hello = (SayHello) factory.getBean("hello"); 

Twoja fasola SayHello ma wartość null.

+0

Dzięki @Aubin, natknąłem się na wiele przykładów, które polegają tylko na applicationContext.xml do obsługi fasoli. Uważam, że samouczek używa tej metody, ponieważ używa Spring 2. – cast01

Powiązane problemy