ZumiKua
ZumiKua

Reputation: 506

Can not find symbol when using assertThatThrownBy with a lambda which has unchecked assignment

I'm using JUnit and AssertJ to test my code, and here is the test:

public class FooTest {
    @Test
    public void test(){
        Foo f = new Foo();
        assertThatThrownBy(() -> f.abc(Collections.EMPTY_MAP)).isInstanceOf(RuntimeException.class)
                .hasMessageContaining("test");
    }
}

the code of Foo is as follow:

public class Foo {
    public void abc(Map<Integer, String> ss){
        throw new RuntimeException("test");
    }
}

When I compile above code, a error is thrown, which says javac can't found symbol hasMessageContaining in AbstractAssert, but if I change Collections.EMPTY_MAP to new HashMap(), everything works fine.

It seems that isInstanceOf returns AbstractAssert rather than AbstractThrowableAssert.

And, if I change lambda to anonymous class, above error vanished either.

So, where is the problem of my code?

Upvotes: 0

Views: 418

Answers (1)

Joel Costigliola
Joel Costigliola

Reputation: 7116

the problem is that EMPTY_MAP is a raw type (Map), if you use emptyMap it should work fine.

Upvotes: 1

Related Questions