简体   繁体   English

单元测试像 doFilter 这样的 void 方法的好处或目的是什么?

[英]Whats the benefit or purpose of unit testing a void method like doFilter?

I am on the java tomcat stack and creating a new filter.我在 java tomcat 堆栈上并创建了一个新的过滤器。 https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpFilter.html I am interested in unit testing it because I want to have 100% branch coverage. https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpFilter.html我对单元测试很感兴趣,因为我想要 100% 的分支覆盖率。

This filter wraps the response object.此过滤器包装响应对象。 We override the default behavior of the response such that whenever we call response.addCookie(cookie), we append the string "happy" to the cookie name:我们覆盖响应的默认行为,这样每当我们调用 response.addCookie(cookie) 时,我们都会将字符串“happy”附加到 cookie 名称:

HappyCookieFilter implements Filter {
HappyCookieResponseWrapper happyCookieResponseWrapper;
...

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) {
    chain.doFilter(req, happyCookieResponseWrapper.wrap(res));
}

...
}
  1. Assuming the HappyCookieResponseWrapper is already unit tested, what would be the benefit of testing doFilter method?假设 HappyCookieResponseWrapper 已经过单元测试,测试 doFilter 方法有什么好处?
  2. How would I test the HappyCookieFilter.doFilter and what should I assert?我将如何测试 HappyCookieFilter.doFilter 以及我应该断言什么?

"What would be the benefit of testing doFilter method?" “测试doFilter方法有什么好处?”

None!没有任何!

Quoting answer to the question " Should unit tests be written for getter and setters? ":引用问题“是否应该为 getter 和 setter 编写单元测试? ”的答案:

Unit tests are there to test the behaviour of your code, ...单元测试用于测试代码的行为,...

There is really no behavior to be tested in that Filter code.在那个Filter代码中确实没有要测试的行为。 The behavior to be tested is in the HappyCookieResponseWrapper class, and you're already testing that.要测试的行为在HappyCookieResponseWrapper类中,您已经在测试它。 Repeating that test would just be a waste of time.重复那个测试只会浪费时间。


"I want to have 100% branch coverage" “我想要 100% 的分支机构覆盖率”

Quoting a different part of the same answer above:引用上面相同答案的不同部分:

@Will said you should aim for 100% code coverage, but in my opinion that's a dangerous distraction. @Will 说你应该瞄准 100% 的代码覆盖率,但在我看来,这是一种危险的分心。 You can write unit tests that have 100% coverage, and yet test absolutely nothing.您可以编写具有 100% 覆盖率的单元测试,但绝对不进行任何测试。

If the doFilter() method has 'interesting' logic like special filtering or routing rules, testing them whould be the purpose of testing this void method.如果doFilter()方法具有“有趣”的逻辑,例如特殊过滤或路由规则,则测试它们应该是测试此 void 方法的目的。 Generally speaking, if a void method has logic to create side effecs (apart from just calling chain.doFilter() in your case) then the purpose of testing said void method would be to test that logic.一般来说,如果 void 方法具有创建副作用的逻辑(除了在您的情况下仅调用chain.doFilter() ),那么测试所述 void 方法的目的将是测试该逻辑。 On the other hand this logic could and should live in a non-void method (maybe in another class) that could be tested on it's own.另一方面,这个逻辑可以也应该存在于一个非空的方法中(可能在另一个类中),它可以自己测试。

Having 100% coverage (line or branch) is nothing to aim for (especially if you don't make sure that the code isn't only covered but also asserted).拥有 100% 的覆盖率(行或分支)并不是什么目标(特别是如果您不确保代码不仅被覆盖而且还被断言)。

It is a perfectly valid class and method to unit test.它是一个完全有效的单元测试类和方法。

Test in chain.doFilter whether (the intended behavior of happyCookieResponseWrapper.wrap(response) was passed as request. No technicalities. Maybe that a cookie was set in the request or so. The business logic and functioning of happyCookieResponseWrapper need not be repeated. There might be an elementary overlap (cookie was set), but that happens at two different levels.chain.doFilter测试( happyCookieResponseWrapper.wrap(response)的预期行为是否作为请求传递。没有技术happyCookieResponseWrapper.wrap(response) 。也许在请求中设置了一个 cookie。happyCookieResponseWrapper 的业务逻辑和功能不需要重复。有可能是基本重叠(设置了 cookie),但这发生在两个不同的级别。

By the way I am a bit surprised seeing a response wrapper for a request, but as it compiles it'll be alright.顺便说一句,看到请求的响应包装器让我有点惊讶,但是当它编译时就可以了。

It is a very narrow/trivial check.这是一个非常狭窄/琐碎的检查。 But assume this code at some time in the future is redesigned to use an other wrapper class, that does not fulfill the correct behavior.但是假设此代码在未来的某个时间被重新设计以使用其他包装类,这不符合正确的行为。 The regression error will then be found fast, as opposed to hearing from a fuzzily described error and processing a ticket.然后将快速找到回归错误,而不是从模糊描述的错误中听到并处理票证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM