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

前端 未结 7 899
太阳男子
太阳男子 2020-11-28 09:37

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 bot

7条回答
  •  借酒劲吻你
    2020-11-28 09:54

    Try invoking on an instance of file not on an instance of class File

    E.g.

    File file = ...;    
    field.setChar(file,'/');
    

    You could also try http://code.google.com/p/jmockit/ and mock the static method FileSystem.getFileSystem(). (don't know if you can mock static variables, normally those hacks shouldn't be necessary -> write oo code and use 'only' mockito)

提交回复
热议问题