Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Null Analysis and Unit Tests
Null Analysis and Unit Tests [message #1219196] Mon, 02 December 2013 15:24 Go to next message
Philippe Marschall is currently offline Philippe MarschallFriend
Messages: 120
Registered: July 2009
Senior Member
Hi

We're evaluating the null analysis in JDT and we encountered some issues
that are annoying in unit tests. The cause is that null analysis doesn't
seem to understand that certain methods should be treated similar to a
constructor in the sense that they always run before other methods. Such
examples are JUnit setUp methods but also @PostConstruct methods.

Consider the following case:
- tests and the code under test are in the same project (Maven default)
- tests and the code under test are in the same package (Maven default
and handy for things like package default visibility)
- @NonNullByDefault on the package
- a test that look like this

public class ATest {

private SomeType object;

@Before
public void setUp() {
object = new SomeType();
}

@Test
public void aMethod() {
assertNotNull(object.aMethod());
}

}

This test is null safe because the @Before method runs after the
constructor and before every other method but null analysis does not
seem to understand this. In this case the problem could be solved by
initializing in the declaration. But sometimes the set up is more
involved and you want it in a dedicated method.

Are there any workarounds like disabling null analysis for certain
source folders or classes?

Cheers
Philippe
Re: Null Analysis and Unit Tests [message #1220575 is a reply to message #1219196] Thu, 12 December 2013 15:22 Go to previous messageGo to next message
Philippe Marschall is currently offline Philippe MarschallFriend
Messages: 120
Registered: July 2009
Senior Member
On 02.12.13 16:24, Philippe Marschall wrote:
> Hi
>
> We're evaluating the null analysis in JDT and we encountered some issues
> that are annoying in unit tests. The cause is that null analysis doesn't
> seem to understand that certain methods should be treated similar to a
> constructor in the sense that they always run before other methods. Such
> examples are JUnit setUp methods but also @PostConstruct methods.
>
> Consider the following case:
> - tests and the code under test are in the same project (Maven default)
> - tests and the code under test are in the same package (Maven default
> and handy for things like package default visibility)
> - @NonNullByDefault on the package
> - a test that look like this
>
> public class ATest {
>
> private SomeType object;
>
> @Before
> public void setUp() {
> object = new SomeType();
> }
>
> @Test
> public void aMethod() {
> assertNotNull(object.aMethod());
> }
>
> }
>
> This test is null safe because the @Before method runs after the
> constructor and before every other method but null analysis does not
> seem to understand this. In this case the problem could be solved by
> initializing in the declaration. But sometimes the set up is more
> involved and you want it in a dedicated method.
>
> Are there any workarounds like disabling null analysis for certain
> source folders or classes?

*bump*

Cheers
Philippe
Re: Null Analysis and Unit Tests [message #1238652 is a reply to message #1220575] Sun, 02 February 2014 01:05 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1051
Registered: July 2009
Senior Member
Sorry for the delay in answering (we are more than busy getting JDT ready for Java 8 ).

The question you ask poses some difficulties:

Null analysis for fields is not at all straight forward. To help the compiler really understanding your scheme of initialization more annotations need to be included in the analysis (which we cannot offer within the Luna time frame). I don't think JUnit's @Before annotation would be first on our list, because avoiding NPE in an application seems more relevant than avoiding NPE in tests Smile

Of course there are means to suppress warnings (and if suppressing optional errors is enabled: errors, too) at several levels. You can ignore all optional problems per source folder, or you could simply add @SuppressWarnings("null") to your test class.

Stephan

Re: Null Analysis and Unit Tests [message #1278372 is a reply to message #1238652] Thu, 27 March 2014 08:36 Go to previous message
Philippe Marschall is currently offline Philippe MarschallFriend
Messages: 120
Registered: July 2009
Senior Member
On 02.02.14 02:05, Stephan Herrmann wrote:
> Sorry for the delay in answering (we are more than busy getting JDT
> ready for Java 8 ).
>
> The question you ask poses some difficulties:
>
> Null analysis for fields is not at all straight forward. To help the
> compiler really understanding your scheme of initialization more
> annotations need to be included in the analysis (which we cannot offer
> within the Luna time frame). I don't think JUnit's @Before annotation
> would be first on our list,

Fair enough. What about @PostConstruct? Yes, that would require a change
to JUnit to support @PostConstruct (and @PreDestroy) but that may be
better in the long run anyway.

> because avoiding NPE in an application seems
> more relevant than avoiding NPE in tests :)
>
> Of course there are means to suppress warnings (and if suppressing
> optional errors is enabled: errors, too) at several levels. You can
> ignore all optional problems per source folder, or you could simply add
> @SuppressWarnings("null") to your test class.

Ok.

Cheers
Philippe
Previous Topic:How to setup WAS in Eclipse
Next Topic:Reconciling the document partitioner, reconcilers and resource builder
Goto Forum:
  


Current Time: Mon Dec 22 15:10:37 GMT 2014

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

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