我最近发现了用于MVC测试的Spring项目:spring-test-mvc.这是一个很棒的工具,我计划将来更多地使用它.
但是我注意到我的Jenkins CI上有问题.问题是当MVC集成测试在本地传递,甚至在Jenkins CI工作时,问题发生在Jenkins的Sonar插件执行中.在这种情况下,所有使用".andExpect()"方法完成的断言我尝试失败.是的,如果不使用Sonar插件,它们会通过.
例如
this.mockMvc.perform(get("/someController/some.action").param("someParam", "someValue"))
.andExpect(status().isOk())
.andExpect(content().type(MediaType.APPLICATION_JSON))
.andExpect(request().sessionAttribute("someAttribute", notNullValue()));
Run Code Online (Sandbox Code Playgroud)
在上面的测试中,内容类型和会话属性断言失败.有任何想法吗?提前致谢.
AngularJS使用REST对服务器端进行身份验证,并获取JSESSIONID cookie.在下一步中,我尝试使用REST从服务器端获取一些JSON数据以及在上一步中获得的会话cookie.这是客户端代码:
getSomeJSONDataFromServer:function() {
var deferred = $q.defer();
$http({
method: 'POST',
withCredentials: true,
url: "http://domain.name/app/someURL",
headers:{
'Accept':'application/json',
'Content-Type':'application/json; charset=utf-8',
'Access-Control-Request-Headers': 'X-Requested-With, content-type, accept, origin, withcredentials'
}
})
.success(function(data, status, headers, config) {
// handle data
})
.error(function(data, status, headers, config) {
// handle error
});
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常:

当我在上面的POST请求体中发送一些数据时,问题就开始了.
...
$http({
method: 'POST',
withCredentials: true,
url: "http://domain.name/app/someURL",
headers:{
'Accept':'application/json',
'Content-Type':'application/json; charset=utf-8',
'Access-Control-Request-Headers': 'X-Requested-With, content-type, accept, origin, withcredentials'
},
data: '{}'
})
.success(...
Run Code Online (Sandbox Code Playgroud)
上述代码在prelight请求中失败:

看起来服务器启动一个新会话,因为会话cookie由于某种原因没有发送.无论如何,我觉得我错过了一些非常简单的东西,一些标题或类似的东西...任何想法都很感激.提前致谢.