Automatically add properties when running JUnit in Eclipse

后端 未结 5 910
猫巷女王i
猫巷女王i 2021-02-04 05:08

In order to run my unit tests on my Eclipse, I need to set some properties for the VM.

Thus, when I first run my JUnit test, I go in \"Open Run Dialog\", then in my JUni

5条回答
  •  爱一瞬间的悲伤
    2021-02-04 05:41

    My solution is to create an abstract test base class for all tests in a project which extends TestCase. It has to be abstract so the automatic unit test finder will not consider it.

    In static code block of this class, I set all properties I need. This ensures that the code runs once and only once and that it runs before any test in my project.

    [EDIT] You say that Spring is initialized before the tests run. This is a bug in your project: It must be the tests who initialize Spring. Otherwise, you will always run into the problem that you have to test something outside of your control.

    Therefore, I suggest to move the Spring init code into a place where you can call it at the point in time when the environment is ready.

    Alternatively, check that the environment is correctly set up in setUp() and throw an error if a property is missing. This way, you will at least know why the tests would fail later. But I still prefer to have total control when which subsystem comes to life. Anything else just begs for disaster.

提交回复
热议问题