Eclipse Tip: Keep static imports for JUnit 4

12 Oct, 2008
Xebia Background Header Wave

If you are using Eclipse to write your JUnit 4 tests you might have noticed that when you organize the imports, the line

import static org.junit.Assert.*;

at the top of your JUnit 4 classes is replaced by stuff like:

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

While your code still compiles and runs OK, this forces you to add another specific static import every time you want to use another static assert method like assertEquals.
Luckily this can be fixed by going to Windows/Preferences…, going to the Java/Code Style/Organize Imports tab and then entering 1 where it says "Number of static imports needed for .*" and then clicking the OK button.
Hope this helps!


Get in touch with us to learn more about the subject and related solutions

Explore related posts