سؤال

My unit tests use the main entry point in each of my Java classes. I'm using Eclipse. It's been risky to rely on the test results because sometimes I've forgotten to set -enableassertions in the JVM arguments. Is there a universal setting that applies to all current and future Run Configurations?

هل كانت مفيدة؟

المحلول

To make assertions enabled by default, first go to the Eclipse menu named Window because Eclipse is a GUI application so it seems to be an excellent reason to put it in a menu called "Window".

Window > Preferences > Java > Installed JREs

Select the JRE that you use.

Click Edit. Add to the Default VM Arguments textbox the option -enableassertions. Click Apply if necessary.

Now, even without enabling assertions in the launch configuration, which seems to be synonymous with run configuration, programs run in Eclipse will have assertions enabled.

نصائح أخرى

Please don't use the Java assert in unit tests.

This kind of assertion can be disabled because it is not intended to be used in unit tests at all. This assert only helps you to ensure some conditions at runtime of your actual code. Unit tests are different and done with special means as like JUnit

There are plenty good ressources to learn about proper unit testing, I would like to propose this as a possible starting point.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top