Unit test using the Reflections google library fails only when executed by Maven

前端 未结 5 1210
有刺的猬
有刺的猬 2020-12-18 00:00

I am using the Google Reflections library for querying certain resources in the classpath. Those resources are located in the same location than the classes in my project.

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 00:31

    There are a few issues you can create that makes surefire fail.

    1. there is a naming convention; test suites should be called 'TestBlaBla' or 'BlaBlaTest'; beginning or ending with the word 'Test'.

    2. as mentioned earlier, the classpath in Maven is more restricted than it is in Eclipse as Eclipse (stupidly) does not separate the compile classpath from the test classpath.

    3. Surefire is free to run test cases from different test suites in any order. When running multiple test suites that initialize some common base (such as an in-memory database or a JNDI context) that can create conflicts where test suites start to influence each other. You need to take care to properly isolate test suites. Tricks I use are to use separate in-memory databases for suites, and I initialize shared things per unit test in stead of per test suite.

    3 is the hardest to debug I can tell you; whenever something works in Eclipse and not in Maven I naturally assume I'm doing something wrong in isolating the test suite.

提交回复
热议问题