2013-01-21 143 views
1

我正在使用嵌入式glassfish進行集成測試。我的網絡服務用球衣+春季編寫。從IDE運行集成測試Spring

我已經爲類AuthFacadeBean編寫了測試。當我通過maven運行測試時 - 所有測試都成功通過了。但是當我在IDE中運行測試時 - 返回NullPointerException。

的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" ... > 
    <context:component-scan base-package="api.facade" /> 
    <bean id="authService" scope="singleton" class="api.management.AuthServiceImpl" /> 
</beans> 

AuthServiceImpl:

public interface AuthService { 
    String login (String secretKey, Integer userId, String accessToken); 
} 

AuthFacadeBean:

@ Component 
@ Scope ("request") 
@ Path ("/ auth") 
@ Produces (MediaType.APPLICATION_JSON) 
public class AuthFacadeBean { 

    @ Autowired 
    AuthService authService; 

    @ GET 
    public Response auth (@ QueryParam ("secretKey") String secretKey, 
                       @ QueryParam ("userId") Integer userId, @ QueryParam ("accessToken") String accessToken) { 
        authService.login (secretKey, userId, accessToken); // exception in this line 
        return Response.ok(). build(); 
    } 
} 

測試:

public class AuthIntegrationTest extends AbstractIntegrationTest { 

    @Test 
    public void testAuth() { 
     URI url = UriBuilder.fromUri(getEndpoint()).path("api/auth") 
       .queryParam("userId", 1) 
       .queryParam("accessToken", "qwe") 
       .queryParam("secretKey", "secret") 
       .build(); 
     ClientResponse response = getClient().resource(url).get(ClientResponse.class); 
     Assert.assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); 
    } 
} 

棧跟蹤:

янв 21, 2013 12:35:02 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException 
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container 
java.lang.NullPointerException 
    at api.facade.AuthFacadeBean.auth(AuthFacadeBean.java:31) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    ... 

回答

0

問題在pom.xml中。需要加彈簧測試:

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

您是否添加了一些默認的配置文件(如開始測試運行時嵌入容器)上的pom.xml即使在M2_HOME/conf目錄Setting.xml的,當你運行測試在IDE中,默認配置文件未被觸發。

+0

不,我使用默認配置文件。我可以使用此配置文件從IDE運行Maven構建,所有測試都已成功通過。 –