How to JUnit test for object immutabily?
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.
精彩评论