开发者

set xml data in spring configuration for Junit test

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?


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:

ClassPathResource res = new ClassPathResource("/testing/test1/doc2.xml");
InputStream is = res.getInputStream();

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:

@Resource(name="test1ValidDocuments")
private List test1ValidDocs;

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/resources will be visible during the testing phase only (mvn test).


You should be able to use any xml property value in Spring config if you wrap it in CDATA xml construct.

See Wikipedia entry


I totally agree with @Mike solution I just wanted to say it's not necessary to use spring for your test unless you're already injecting beans within your test. your method should do the job with no spring context awareness so it will be reusable and test execution is faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜