Using reflection to change static final File.separatorChar for unit testing?

Posted by Stefan Kendall on Stack Overflow See other posts from Stack Overflow or by Stefan Kendall
Published on 2010-03-18T23:20:43Z Indexed on 2010/03/18 23:31 UTC
Read the original article Hit count: 468

Filed under:
|
|
|

Specifically, I'm trying to create a unit test for a method which requires uses File.separatorChar to build paths on windows and unix. The code must run on both platforms, and yet I get errors with JUnit when I attempt to change this static final field.

Anyone have any idea what's going on?

Field field = java.io.File.class.getDeclaredField( "separatorChar" );
field.setAccessible(true);
field.setChar(java.io.File.class,'/');

When I do this, I get

IllegalAccessException: Can not set static final char field java.io.File.separatorChar to java.lang.Character

Thoughts?

© Stack Overflow or respective owner

Related posts about reflection

Related posts about unit-testing