天天看點

Mockito 比對參數

– Start

package demo06;

import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

@SuppressWarnings("unchecked")
class ArgumentMatchersTest {

	@Test
	void test() {
		// mock creation
		List<String> data = Mockito.mock(List.class);

		// stubbing, 比對任何 int 參數
		Mockito.when(data.get(ArgumentMatchers.anyInt())).thenReturn("test");

		// verification
		Assertions.assertEquals("test", data.get(0));
		Assertions.assertEquals("test", data.get(10));
	}

}

           

– 更多參見:Mockito 精萃

– 聲 明:轉載請注明出處

– Last Updated on 2019-08-15

– Written by ShangBo on 2019-08-15

– End

繼續閱讀