开发者

How to JUnit test for object immutabily?

开发者 https://www.devze.com 2023-02-20 12:39 出处:网络
I have a method similar to this public void me开发者_Go百科thod(final Object A){ .... } now I want to write a test which ensures Object A is always final. How do I write such test ?I\'m afraid you

I have a method similar to this

public void me开发者_Go百科thod(final Object A){ .... }

now I want to write a test which ensures Object A is always final. How do I write such test ?


I'm afraid you do not understand what your method does. A final parameter in a method means only that the parameter cannot be reassigned in the method.

 public void doStuff( String s ){
     s = "OK";
 }

but

 public void doStuff( final String s ){
     s = "ERROR s cannot be assigned because it is final!";//causes error
 }

As such, there is no behaviour that can test for. If it is not valid it will not compile.


Edit: If you want you unit test for a final I don't that is possible either since I believe that information is lost at time of compilation once it is checked internally.

0

精彩评论

暂无评论...
验证码 换一张
取 消