Skip to main content



      Home
Home » Eclipse Projects » Eclipse Scout » Coding Guidline: Assert static import or not
Coding Guidline: Assert static import or not [message #1018628] Thu, 14 March 2013 04:22 Go to next message
Eclipse UserFriend
When writing JUnit tests there are two possibilities for importing the Assert class:

Solution 1: static import.

It is possible to use:
import static org.junit.Assert.*


I think Eclipse convert this to
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;


This allows calling assertXxxxx() functions directly in the code. For example:
assertTrue(emptyList.isEmpty());




Solution 2: normal imports.

Assert is imported with a normal import:
import org.junit.Assert;


In the code assertXxxxx() are used with Assert class:

Assert.assertTrue(emptyList.isEmpty());




For the moment, we are using solution 2 (we had a mix of both solutions, and when I pushed code for Bug 402301, Bug 402336, Bug 402314... I migrated everything to use solution 2). It is possible to change it back to solution 1 (it is a search and replace operation + organize imports).

I do not care if we use solution 1 or 2.

I think we should agree on something. Define it as guideline. Add a Checkstyle check to ensure that every test is written the same way.
Re: Coding Guidline: Assert static import or not [message #1018645 is a reply to message #1018628] Thu, 14 March 2013 04:55 Go to previous messageGo to next message
Eclipse UserFriend
I prefer solution 1 (using static imports) to solution 2 because it shortens the code and improves readability.
Since we write test code, the context is clear and if you encounter an "assertTrue" method call, you generally know immediately that this belongs to the Assert class of the JUnit package.
Re: Coding Guidline: Assert static import or not [message #1018650 is a reply to message #1018628] Thu, 14 March 2013 05:00 Go to previous messageGo to next message
Eclipse UserFriend
I agree with Ken.

I think that you should avoid static imports in normal code, because one cannot see directly where the method is defined. min(a, b) might be unclear, while Math.min(a, b) is not.

However, JUnit code is clearly different from "production code" and using the assert* methods is an intristic property of such code. If you look at it, you expect the use of assert* methods and are not confused about it. You can add org.junit.Assert.* as a favorite in your Eclipse preferences (Java > Editor > Content Assist > Favorites) to ease the use of those imports.

Beat
Re: Coding Guidline: Assert static import or not [message #1018662 is a reply to message #1018650] Thu, 14 March 2013 05:16 Go to previous messageGo to next message
Eclipse UserFriend
I we decide to migrate the code to solution 1, we need to open a new bug to handle the change (including configuration of checkstyle).

See
Checkstyle: AvoidStarImport
Similar discussion at Tomcat
Re: Coding Guidline: Assert static import or not [message #1018698 is a reply to message #1018662] Thu, 14 March 2013 06:24 Go to previous messageGo to next message
Eclipse UserFriend
Basically in business code i would definitely avoid static imports due to naming conflicts and potential ambiguous name resolution.

In Test code however especially for Assert.* i understand that it could be useful.
But when it comes to coding guide lines i would be consequent and avoid it.

In bsi we have a coding guide line for all our products that does not allow for static imports.
Re: Coding Guidline: Assert static import or not [message #1019171 is a reply to message #1018698] Fri, 15 March 2013 03:08 Go to previous messageGo to next message
Eclipse UserFriend
I aggree that we should allow static imports for org.junit.Assert. We could exclude that from the checkstyle rule.
Re: Coding Guidline: Assert static import or not [message #1019419 is a reply to message #1019171] Fri, 15 March 2013 12:26 Go to previous messageGo to next message
Eclipse UserFriend
I my opinion it is not about allowing both solutions (that was already the case). It is about defining a coding guideline for all our tests. If we choose the "Solution 1" we need to extend our checkstyle configuration to check for normal imports "import org.junit.Assert;" and mark them as violations (I propose INFO level - like for magic numbers, with the message "consider using static import for org.junit.Assert, it improves readability").

I am not sure if the configuration mentioned by beat is a workspace configuration or a project configuration. If it is a project configuration, we need to add it to the shared configuration file.
Re: Coding Guidline: Assert static import or not [message #1041835 is a reply to message #1019419] Mon, 15 April 2013 12:39 Go to previous message
Eclipse UserFriend
I have opened Bug 405739 to change to static imports.
Previous Topic:<<queueing rwt runnable into rwt thread:>>
Next Topic:SWT: Opening multiple instances of a form in a view/editor
Goto Forum:
  


Current Time: Thu Jul 24 18:07:14 EDT 2025

Powered by FUDForum. Page generated in 0.04747 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top