Using reflection to change static final File.separatorChar for unit testing?
- by Stefan Kendall
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?