java JUnit 集成测试应报告 HTTP 请求/响应详细信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27892119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit integration tests should report HTTP request/response details
提问by Alejandro
I'm writing some Integration Tests (ITs) that are being run using Maven Failsafe plugin.
我正在编写一些使用 Maven Failsafe 插件运行的集成测试 (IT)。
In short, these ITs perform an HTTP call and analyze the JSON response to ensure certain elements are present.
简而言之,这些 IT 执行 HTTP 调用并分析 JSON 响应以确保存在某些元素。
When a test fails, I would like to be able to see the details of the HTTP request and response (headers, body, etc.), not just the assertion failure message.
当测试失败时,我希望能够看到 HTTP 请求和响应的详细信息(标头、正文等),而不仅仅是断言失败消息。
If my test looks something like this:
如果我的测试看起来像这样:
public class FooBarTest {
MyHttpClient httpClient;
@Before
public void setupHttpClient(){
this.httpClient = ...
}
@Test
public void testFooBarBaz(){
Response response = this.httpClient.get("http://some/url");
Assert.assertEquals(200, response.getStatus());
String json = response.getBody();
Assert.assertEquals("baz", evalJsonPath(json, "foo.bar.id"));
}
}
When the test is run from command line (through Maven), I want to be able to see the assertion error and additionally the request and response details. I assume this requires printing to System.out/err but it's better done through some Logging system.
当从命令行(通过 Maven)运行测试时,我希望能够看到断言错误以及请求和响应的详细信息。我认为这需要打印到 System.out/err 但最好通过一些日志系统来完成。
Additionally, I want the same information to be available in the test TXT report file that surefire/failsafe produces:
此外,我希望在 surefire/failsafe 生成的测试 TXT 报告文件中提供相同的信息:
-------------------------------------------------------------------------------
Test set: com.sample.FooBarTest
-------------------------------------------------------------------------------
REQUEST: {...}
RESPONSE: {...}
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.273 sec <<< FAILURE! - in com.sample.FooBarTest
testFooBarBaz(com.sample.FooBarTest) Time elapsed: 3.27 sec <<< ERROR!
junit.framework.AssertionFailedError: expected:<200> but was:<404>
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.failNotEquals(Assert.java:283)
at junit.framework.Assert.assertEquals(Assert.java:64)
at junit.framework.Assert.assertEquals(Assert.java:195)
at junit.framework.Assert.assertEquals(Assert.java:201)
at com.sample.FooBarTest.testFooBarBaz(FooBarTest.java:XX)
Finally, the same details should be present in some fashion in the XML report, in a way that Jenkins displays this information as well when drilling down into failed tests pages.
最后,同样的细节应该以某种方式出现在 XML 报告中,这样 Jenkins 在深入到失败的测试页面时也会显示这些信息。
If at all possible, I only care about printing this information when there are failed tests.
如果可能的话,我只关心在测试失败时打印此信息。
How can I accomplish this? I've started looking into these options, but more guidance would be appreciated
我怎样才能做到这一点?我已经开始研究这些选项,但希望得到更多指导
- Custom JUnit reporter, runner or listener
- JUnit @Rules (Method/Class Rule, ErrorCollector, etc.)
- Using some special logger
- ...
- 自定义 JUnit 报告器、运行器或侦听器
- JUnit @Rules(方法/类规则、ErrorCollector 等)
- 使用一些特殊的记录器
- ...
PS. I'm not looking for a solution that simply writes such details to a separate file since the I consider the implications of that to be somewhat less user-friendly.
附注。我不是在寻找一种解决方案,它只是将此类详细信息写入单独的文件,因为我认为这样做的含义对用户不那么友好。
Thanks!
谢谢!
回答by Joe
You can use Hamcrest (Why should I use Hamcrest-Matcher and assertThat() instead of traditional assertXXX()-Methods) with a custom Matcher<Response>
(how to implement a hamcrest matcher, Is there a simple way to match a field using Hamcrest?).
您可以使用 Hamcrest(为什么我应该使用 Hamcrest-Matcher 和 assertThat() 而不是传统的 assertXXX()-Methods)与自定义Matcher<Response>
(如何实现 hamcrest 匹配器,是否有一种简单的方法来使用 Hamcrest 匹配字段?)。
With a little work on the Matcher
s, this will allow you to write your test as:
在Matcher
s上做一些工作,这将允许您将测试编写为:
assertThat(response, hasStatus(200));
assertThat(response, hasJsonPathEval("foo.bar.id", eq("baz"));
and any failure will include the toString
representation of your response
.
并且任何失败都将包括toString
您的response
.
This is functionally equivalent to:
这在功能上等同于:
assertEquals(response.toString(), 200, response.getStatus());
but makes clearer that you expect the response
and a description of the test to be shown on failure.
但更清楚的是,您希望response
在失败时显示测试的和 描述。
回答by dkatzel
All you need to do is to make a custom error message to print whatever you want
您需要做的就是制作自定义错误消息以打印您想要的任何内容
assertEquals( getResponseDetails(response), 200, response.getStatus());
...
assertEquals( getResponseDetails(response), "baz", evalJsonPath(json, "foo.bar.id"));
Where getResponseDetails()
is a method that you write.
getResponseDetails()
你写的方法在哪里。
private String getResponseDetails(Response response){
StringBuider builder = new StringBuilder();
buider.append("header = '").append(response.getHeader()).append("'\n");
...//similar code for body etc
return builder.toString();
}
This String will be written to wherever you have configured the tests output to go in your Maven configuration and will only be printed when the test fails.
该字符串将被写入您在 Maven 配置中配置测试输出的任何位置,并且仅在测试失败时才会打印。
Or optionally since it appears Response
may be a custom class that you created, you can make Response#toString()
contain the information you want in which case you just have to do this:
或者,由于它Response
可能是您创建的自定义类,因此您可以选择Response#toString()
包含所需的信息,在这种情况下,您只需执行以下操作:
assertEquals( response, 200, response.getStatus());
...
assertEquals( response, "baz", evalJsonPath(json, "foo.bar.id"));
As a side note: I notice from your stack trace you are using junit.framework.Assert
which is the old JUnit 3 library. You should use JUnit 4's org.junit.Assertinstead
附带说明:我从您的堆栈跟踪中注意到您使用的junit.framework.Assert
是旧的 JUnit 3 库。您应该使用JUnit 4的org.junit.Assert代替