一种奇怪的错误,执行一个junit测试用例,代码却执行了很多遍
程序员文章站
2022-04-05 17:17:46
...
最近用junit做接口测试的时候,项目框架是ssm,只执行了一次测试用例,但是service实现层根据日志,发现执行了几十次到上百次,直到栈溢出才会停止。
这是测试用例:
@Test
public void testGetTest() {
try {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
String url = "http://localhost:8080/project/test/getTest";
HttpPost post = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("testid", "100001"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
post.setEntity(entity);
CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(post);
if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String result = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
System.out.println(result);
}
closeableHttpResponse.close();
closeableHttpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
接口:
@RequestMapping(value = "/getTest", method = { RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public Map<String, Object> getTest(String testid) throws Exception {
TestDetail message = new TestDetail ();
if(!TestUtils.validateNumber(testid, -1)) {
message.setMsg("id不合法");
message.setResult(1);
return PropertyUtilsExtend.describe(message);
}
message = this.testService.getTestById(Long.valueOf(testid));
if(message == null) {
message = new TestDetail();
message.setMsg("获取信息失败");
message.setResult(99);
} else {
message.setMsg("获取信息成功");
message.setResult(0);
}
return PropertyUtilsExtend.describe(message);
}
实现层:
@Override
@Transactional(readOnly = true)
public TestDetail getTestById(long testid) {
TestDetail test= this.testDao.query(testid);
test.setItems(this.testmodeDao.query(testid));
return null;
}
执行测试用例:
图片中显示测试用例一直在执行。
console循环输出信息:
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[javax.servlet-api-3.1.0.jar:3.1.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[javax.servlet-api-3.1.0.jar:3.1.0]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:845) ~[?:?]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1689) ~[?:?]
at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:64) ~[log4j-web-2.5.jar:2.5]
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1676) ~[?:?]
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:581) ~[?:?]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) ~[?:?]
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:566) ~[?:?]
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226) ~[?:?]
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1180) ~[?:?]
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:511) ~[?:?]
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) ~[?:?]
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1112) ~[?:?]
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) ~[?:?]
at org.eclipse.jetty.server.Dispatcher.include(Dispatcher.java:117) ~[?:?]
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:160) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1246) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1029) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:973) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
最后栈溢出:
Caused by: java.lang.*Error
at java.lang.String.startsWith(String.java:1434) ~[?:1.8.0_91]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.setAttribute(Dispatcher.java:461) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.removeAttribute(Dispatcher.java:494) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.setAttribute(Dispatcher.java:469) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.removeAttribute(Dispatcher.java:494) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.setAttribute(Dispatcher.java:469) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.removeAttribute(Dispatcher.java:494) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.setAttribute(Dispatcher.java:469) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.removeAttribute(Dispatcher.java:494) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
at org.eclipse.jetty.server.Dispatcher$IncludeAttributes.setAttribute(Dispatcher.java:469) ~[jetty-server-9.3.11.v20160721.jar:9.3.11.v20160721]
没有具体的错误信息,通过排除法判断,发现:
TestDetail test= this.testDao.query(testid);
这句代码执行出错,正常执行,即使报错,也应该只执行一次,但是这里好像一直在循环执行这一句,一直报同样的错误信息(console循环输出信息),知道溢出为止,目前还不知道为什么会出现这个问题,但是可以肯定有代码执行出错了,下次再遇见,也有查找问题的方向了。