【问题标题】:Run integration-test for Spring from IDE从 IDE 运行 Spring 的集成测试
【发布时间】:2013-01-21 08:50:15
【问题描述】:

我正在使用嵌入式 glassfish 进行集成测试。我的 web 服务是用 jersey + 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)
    ...

【问题讨论】:

    标签: java spring glassfish integration-testing


    【解决方案1】:

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

    【讨论】:

    • 不,我使用默认配置文件。我可以使用此配置文件从 IDE 运行 maven build,所有测试都成功通过。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 2015-02-10
      • 2015-09-05
      • 2011-07-11
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      相关资源
      最近更新 更多