I’m unit testing one of my method. The method takes a String object as input. This string object will contain xml data which is then processed by the method. Since my xml input is kind of large, is it possible to set this xml string in a spring configuration file and somehow access it in Jnuit test case. Any other easy way i to access the xml string in my JUnit test?
Share
The best solution would be to put the XML in separate files in the testing classpath and then load them as classpath resources as needed. Spring provides a class called ClassPathResource where you can do this:
That way, your test data isn’t cluttering up your Spring config file. I would recommend defining a java.util.List containing the paths to the files used in a test and then iterating over that. You can pull it automatically from the Spring config like this:
Spring will look for a List bean named test1ValidDocuments in your test context.
If you aren’t already, I’d recommend using Maven for builds and testing. It makes management of this sort of thing dead simple. For example, Maven will manage the classpaths for you. Any files and folders you put in
PROJECT_NAME/src/test/resourceswill be visible during the testing phase only (mvn test).