Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I use org.mockito.AdditionalMatchers.gt?

I'm trying to figure out how org.mockito.AdditionalMatchers works but I failed. Why is this test failing?

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.AdditionalMatchers.*;

public class DemoTest {

    @Test
    public void testGreaterThan() throws Exception {

        assertThat( 17
            , is( gt( 10 ) )
        );
    }
}

Output is:

java.lang.AssertionError: 
Expected: is <0>
     got: <17>
like image 763
Aaron Digulla Avatar asked Feb 22 '13 09:02

Aaron Digulla


1 Answers

You should use Hamcrest's greaterThan for this case. gt is for verifying arguments of method calls in mock objects:

public class DemoTest {

    private List<Integer> list = Mockito.mock(List.class);

    @Test
    public void testGreaterThan() throws Exception {
        assertThat(17, is(org.hamcrest.Matchers.greaterThan(10)));

        list.add(17);
        verify(list).add(org.mockito.AdditionalMatchers.gt(10));
    }

}
like image 86
Christoph Leiter Avatar answered Oct 09 '22 11:10

Christoph Leiter