英文:
Intellij not recognizing fail cases as a successful test
问题
我目前正在一个项目上工作,我已经创建了一些测试来测试服务器的一些失败响应(例如:错误 501、错误 500、错误 414...),但当我运行这些测试时,控制台确实给出了我期望的错误,但是Intellij不将它们视为成功。有人已经经历过这种情况吗?
以下是代码示例:
@Test
void parseHttpRequestBadMethod1 () {
try {
HttpRequest request = httpParser.parseHttpRequest(generateBadTestCaseMethodName());
fail();
} catch (HttpParsingException e) {
assertEquals(e.getErrorCode(), HttpStatusCode.SERVER_ERROR_501_NOT_IMPLEMENTED);
}
}
这是我目前正在工作的项目的 存储库链接。这是一个非常简单的使用 Java 核心的服务器。
我尝试了一些 try/catch 来捕获错误并将其视为成功的测试(因为我的主要目标是验证服务器的错误响应是否有效),但结果不如预期。
英文:
I am currently working on a project where I have created some tests to test some failure responses from the server (e.g.: error 501, error 500, error 414...) but when I run those tests, the console gives me the exactly error I was expecting, but Intellij does not treat them as a success. Has somebody already experienced this?
@Test
void parseHttpRequestBadMethod1 () {
try {
HttpRequest request = httpParser.parseHttpRequest(generateBadTestCaseMethodName());
fail();
} catch (HttpParsingException e) {
assertEquals(e.getErrorCode(), HttpStatusCode.SERVER_ERROR_501_NOT_IMPLEMENTED);
}
}
16:53:34.351 [main] DEBUG com.gui.httpserver.http.HttpParser - Request Line METHOD to Process : GeT
java.lang.RuntimeException: com.gui.httpserver.http.HttpParsingException: Not Implemented
at com.gui.httpserver.http.HttpParser.parseHttpRequest(HttpParser.java:31)
at com.gui.httpserver.http.HttpParserTest.parseHttpRequestBadMethod1(HttpParserTest.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Caused by: com.gui.httpserver.http.HttpParsingException: Not Implemented
at com.gui.httpserver.http.HttpRequest.setMethod(HttpRequest.java:26)
at com.gui.httpserver.http.HttpParser.parseRequestLine(HttpParser.java:63)
at com.gui.httpserver.http.HttpParser.parseHttpRequest(HttpParser.java:29)
... 4 more
Here is the repo for the project I am currently working on. It is a very simple server using java core.
I tried to wrap with some try/catch to try to catch the error and treat them as a successful test (once my main objective is to validate if the errors responses from the server are working) but it did not work as expected.
答案1
得分: 1
我不确定为什么你的代码失败,但我认为更符合习惯的方法是使用测试框架自身的"我们期望在这里抛出异常"功能。如果你正在使用JUnit,你可以在这里找到一个示例。
英文:
I'm not sure why your code fails, but I think a more idiomatic approach is to use the "We expect an exception here" facility of the Test framework itself. If you are using JUnit, you can find an example here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论