开发者

How can I force maven to use my mock version of dependencies during the test phase

so I have a Java application with a that accesses some web services. I want to mock out the appropriate classes so I can just use a few hard coded request/responses and I want maven to seamlessly use them during the tests and ignore them during the final build.

I want maven2 to:

1)compile my mock classes

2)compile the main classes that depend on the mocked code

3)run tests

4)recompile any main classes with the real dependencies, not my mocks

5)finish up the maven install

Ideally I want to just name my mock classes the same as my main class and just keep them in src/test/mocks or something. I'm kind of new to maven and so far it looks like maven will only compile开发者_运维技巧 the src/main and then src/test but I'm hoping I can have my way. Any ideas?


It's not a straight answer to your question, but I believe that it's a better way to do it.

Code to interfaces instead of concrete classes. You would have both production implementations of the interfaces and mock implementations. You could even go wild and write your mocks using jMock or EasyMock. That might be a bit too wild :-)

Use dependency injection to inject production classes in production, and mock classes in your unit tests.

Your mocks can live entirely in ./src/test/java. You can probably add different directories to the test sources, but the path of least resistance would be to just have a package named 'mocks'. Convention over configuration is preferred with Maven.


Make another maven project for these tests. There may be alternatives, but they will be confusing and hard to maintain.


Maven doesn't have such a workflow (especially step 4) and implementing something approaching will definitely not be simple. My recommendation would be to use some kind of DI framework with a "testing configuration" to inject your mocks at runtime during the testing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜