vendredi 7 août 2020

Mockito mock same method calls with different collection-arguments

I try to mock same method calls with different collection-arguments.

My problem is that im not getting the correct mocked-answer from Mocked-Call for the input.

Test-Class:

@ExtendWith(SpringExtension.class)
public class CollectionTest {

    @MockBean
    private Controller c;
    
    @BeforeEach
    public void init() {
        Collection<String> a = Mockito.anyCollection();
        a.add("a");
        Mockito.when(c.run(a)).thenReturn("a");
        
        Collection<String> b = Mockito.anyCollection();
        b.add("b");
        Mockito.when(c.run(b)).thenReturn("b");
    }

    @Test
    public void test() {
        assertEquals("a", c.run(Lists.newArrayList("a"))); // DOESNT'WORK!!! Returns "b" but should "a"
        assertEquals("b", c.run(Lists.newArrayList("b"))); // 
    }
}

Controller-Class:

@Service
public class Controller{
    public String run(Collection<String> c) {
        return "not-mocked";
    }   
}

I'v got no idea why it doesn't return "a". I tried to change the collection to string but same behaviour.

Im using Java Mockito "3.1" and Spring, but I think Mockito is the important information here.

Aucun commentaire:

Enregistrer un commentaire