This surely is a common problem. I have a properties file like my-settings.properties
which is read by an application class. When I write a test class, it needs to test different scenarios of things that could be present in my-settings.properties
in order to ensure maximum code coverage (e.g. empty properties file, basic properties file etc). But I can only have one my-settings.properties
in my src/test/resources
.
What would be really great is if there was just some annotation
@MockFileOnClassPath(use = "my-settings-basic.properties", insteadOf = "my-settings.properties")
Then I could just have multiple my-settings-XXX.properties
files in my /src/test/resources
and just annotated the correct one on each test method. But I can't find anything like this. I'm using JUnit 4.12.
I can think of a couple of crude solutions:
- Before each test, find the file on the file system, copy it using filesystem I/O, then delete it again after the test. But this is clumsy and involves a lot of redundancy. Not to mention I'm not even sure whether the classpath directory will be writable.
- Use a mocking framework to mock
getResource
. No idea how I would even do that, especially as there are a million different ways to get the file (this.getClass().getResourceAsStream(...)
,MyClass.class.getResourceAsStream(...)
,ClassLoader.getSystemClassLoader().getResourceAsStream(...)
etc.)
I just think this must be a common problem and maybe there is already a solution in JUnit, Mockito, PowerMock, EasyMock or something like that?
Aucun commentaire:
Enregistrer un commentaire