Sandra
Sandra

Reputation: 3750

Syntax of hamcrest matchers

In one of our projects I stumbled upon the following line of code (it uses hamcrest matchers 1.3):

assertThat(someReport.getSomeException(), Matchers.<SomeException>notNullValue())

Two questions:

  1. Would somebody please explain this syntax to me? I've never seen the use of < and > in that context.

  2. Eclipse highlights the latter part of that line as en error: The method notNullValue() of type Matchers is not generic; it cannot be parameterized with arguments <SomeException>. However, using maven on the commandline the project builds without problems. So why is there an error?

Upvotes: 2

Views: 278

Answers (1)

John B
John B

Reputation: 32949

  1. This is proper syntax for specifying the generic parameter when calling a static method. If you have the following method...

    public T myMethod();

You could call it as MyClass.<String>myMethod(). In many cases type inference allow for not including the <>. For example the work work for the above: String myVal = MyClass.myMethod().

  1. The javadoc for Matchers has the following signature for notNullValue

    public static Matcher notNullValue()

Notice that the is no generic variable <T> therefore it is not proper to specify one. If it is compiling, I assume it is because the compiler is ignoring it.

Upvotes: 3

Related Questions