gpt4 book ai didi

java - 从 IDE 运行 Spring 集成测试

转载 作者:行者123 更新时间:2023-12-01 04:54:12 25 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 从 IDE 运行 Spring 集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14435149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com