• Home
  • RSS Feed
  • Log in


Eclipse Tip: Keep static imports for JUnit 4
Posted by Vincent Partington in the early evening: October 12th, 2008

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!

Share

Filed under Java | 9 Comments »



9 Responses to “Eclipse Tip: Keep static imports for JUnit 4”



    p3t0r Says:
    Posted at: October 12, 2008 at 7:40 pm

    Also, add ‘org.junit.Assert’ or any of your static helper methods to your ‘favorites’ (preferences, search for favorites). This makes working with static imports much easier.

    Reply


    Martin Tilma Says:
    Posted at: October 13, 2008 at 9:05 am

    I just wrote on the same issue (http://www.func.nl/community/knowledgebase/eclipse-and-static-imports) but the solution differences a bit.

    Reply


    p3t0r Says:
    Posted at: October 13, 2008 at 9:13 pm

    @Martin Tilma … that is exactly what I said in my first comment… but thanks for elaborating ;)

    Reply


    Vincent Partington Says:
    Posted at: October 14, 2008 at 9:13 am

    Hey guys, thanks for that tip! I hadn’t seen that option yet, but it seems an even easier solution to the problem. Especially when other people on your project don’t have the setting I mentioned and reformat the code which removes the star again. (You know who you are! ;-) )

    Reply


    Ron Says:
    Posted at: October 20, 2008 at 2:10 pm

    Great tip!
    cheers, Ron

    Reply


    Eclipse and JUnit 4 Static Imports « Developers, Developers, Developers! Says:
    Posted at: February 12, 2010 at 9:18 pm

    [...] idea taken from http://blog.xebia.com/2008/10/12/eclipse-tip-keep-static-imports-for-junit-4/ Tagged as: Eclipse, JUnit 4 Leave a comment Comments (0) Trackbacks (0) ( subscribe to [...]

    Reply


    Janosch Says:
    Posted at: February 21, 2011 at 1:13 am

    Thanks!
    Had been annoying me for a long while, and this is now over :-) .

    Reply


    Strangeoptics Says:
    Posted at: September 17, 2011 at 4:42 pm

    Exactly what I was searching for.

    I am Writing a Posting about Eclipse shortcuts and the drawback of organise imports.
    Best Of Short Cuts For Eclipse

    Thanks.

    Reply


    Shalom Shachne Says:
    Posted at: December 20, 2011 at 4:43 pm

    Thanks for posting all the tips. Very big help, and increased my productivity right away!

    Reply


Leave a Reply

Click here to cancel reply.


Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India
  • Xebia Sweden

Categories

  • Java (311)
  • Agile (181)
  • General (136)
  • Scrum (67)
  • Architecture (64)
  • Testing (59)
  • Performance (46)
  • Middleware (56)
    • Deployment (38)
  • Xebia Labs (39)
  • SOA (31)
  • Podcast (31)
  • Project Management (28)
  • Tools (26)
  • Uncategorized (20)
  • lean architecture (20)
  • Quality Assurance (17)
  • Articles (13)
  • Requirements Management (13)
  • Virtualization (19)

Tag Cloud

    Grails JPA agile architectuur Frameworks Moving to India Scala Flex Groovy Oracle Javascript ACT Hibernate SOA lean architecture Scrum product owner Concurrency Control Lean Architecture JPA implementation patterns TDD lean architectuur Agile Maven Xebia Java Spring Eclipse XML Ajax

Archives

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
Avatars by Sterling Adventures