1

我正在使用嵌入式 glassfish 进行集成测试。我的 web 服务是用 jersey + spring 编写的。

我已经为 AuthFacadeBean 类编写了测试。当我通过 Maven 运行测试时 - 所有测试都成功通过。但是当我在 IDE 中运行测试时 - 返回 NullPointerException。

应用程序上下文.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)
    ...
4

2 回答 2

0

您是否在 pom.xml 甚至 M2_HOME/conf 下的 setting.xml 中添加了一些默认配置文件(例如在启动测试时运行嵌入容器),并且当您在 IDE 中运行测试时,默认配置文件没有被触发。

于 2013-01-21T09:00:08.907 回答
0

pom.xml 中的问题。需要添加spring-test:

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>${spring.version}</version>
   <scope>test</scope>
</dependency>
于 2013-01-21T11:31:08.737 回答