Deci, am'm a crea un simulacru de obiect ca o variabilă statică pe nivel de clasă asa... Intr-un test, vreau Foo.someMethod()
pentru a reveni o anumită valoare, în timp ce într-un alt test, vreau să se întoarcă o valoare diferită. Problema am'm are este că se pare că am nevoie pentru a reconstrui își bate joc de a obține acest lucru în mod corect. Am'd ca, pentru a evita refacerea își bate joc, și de a folosi doar obiecte de același fel în fiecare test.
class TestClass {
private static Foo mockFoo;
@BeforeClass
public static void setUp() {
mockFoo = mock(Foo.class);
}
@Test
public void test1() {
when(mockFoo.someMethod()).thenReturn(0);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), receiving 0 as the value
}
@Test
public void test2() {
when(mockFoo.someMethod()).thenReturn(1);
TestObject testObj = new TestObject(mockFoo);
testObj.bar(); // calls mockFoo.someMethod(), STILL receiving 0 as the value, instead of expected 1.
}
}
În cel de-al doilea test, am'm a primit 0 ca valoare atunci când testObj.bar() se numește... ceea Ce este cel mai bun mod de a rezolva acest lucru? Rețineți că știu că aș putea folosi o altă mostră de " Foo " în fiecare test, cu toate acestea, trebuie să lant mai multe cereri de pe mockFoo`, adica'd-au de-a face înlănțuirea în fiecare test.
Ai putea, de asemenea, Stub Apeluri Consecutive (#10 din 2.8.9 api). În acest caz, ar trebui să utilizați mai multe thenReturn apeluri sau o thenReturn apel cu mai mulți parametri (varargs).
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
public class TestClass {
private Foo mockFoo;
@Before
public void setup() {
setupFoo();
}
@Test
public void testFoo() {
TestObject testObj = new TestObject(mockFoo);
assertEquals(0, testObj.bar());
assertEquals(1, testObj.bar());
assertEquals(-1, testObj.bar());
assertEquals(-1, testObj.bar());
}
private void setupFoo() {
mockFoo = mock(Foo.class);
when(mockFoo.someMethod())
.thenReturn(0)
.thenReturn(1)
.thenReturn(-1); //any subsequent call will return -1
// Or a bit shorter with varargs:
when(mockFoo.someMethod())
.thenReturn(0, 1, -1); //any subsequent call will return -1
}
}
Mai întâi de toate nu't face machete statice. Face un teren privat. Doar pune-ți setUp class în @Înainte de
nu @BeforeClass
. Acesta ar putea fi făcut mai multe, dar's ieftine.
În al doilea rând, felul în care îl ai acum este modul corect de a obține o mostră de a reveni ceva diferit în funcție de test.
Pentru toți cei care caută să se întoarcă ceva și apoi pentru un alt apel arunca o excepție:
when(mockFoo.someMethod())
.thenReturn(obj1)
.thenReturn(obj2)
.thenThrow(new RuntimeException("Fail"));
sau
when(mockFoo.someMethod())
.thenReturn(obj1, obj2)
.thenThrow(new RuntimeException("Fail"));
Pentru Oricine, folosind spy() și doReturn() în loc de când() metoda:
de ce ai nevoie pentru a reveni obiect diferit pe diferite apeluri este aceasta:
doReturn(obj1).doReturn(obj2).when(this.client).someMethod();
.
Pentru clasic își bate joc de:
when(this.client.someMethod()).thenReturn(obj1, obj2);