org.mockito.Mockito.ignoreStubs()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(229)

本文整理了Java中org.mockito.Mockito.ignoreStubs()方法的一些代码示例,展示了Mockito.ignoreStubs()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Mockito.ignoreStubs()方法的具体详情如下:
包路径:org.mockito.Mockito
类名称:Mockito
方法名:ignoreStubs

Mockito.ignoreStubs介绍

[英]Ignores stubbed methods of given mocks for the sake of verification. Sometimes useful when coupled with verifyNoMoreInteractions() or verification inOrder(). Helps avoiding redundant verification of stubbed calls - typically we're not interested in verifying stubs.

Warning, ignoreStubs() might lead to overuse of verifyNoMoreInteractions(ignoreStubs(...)); Bear in mind that Mockito does not recommend bombarding every test with verifyNoMoreInteractions() for the reasons outlined in javadoc for Mockito#verifyNoMoreInteractions(Object...)Other words: all stubbed methods of given mocks are marked verified so that they don't get in a way during verifyNoMoreInteractions().

This method changes the input mocks! This method returns input mocks just for convenience.

Ignored stubs will also be ignored for verification inOrder, including org.mockito.InOrder#verifyNoMoreInteractions(). See the second example.

Example:

//mocking lists for the sake of the example (if you mock List in real you will burn in hell) 
List mock1 = mock(List.class), mock2 = mock(List.class); 
//stubbing mocks: 
when(mock1.get(0)).thenReturn(10); 
when(mock2.get(0)).thenReturn(20); 
//using mocks by calling stubbed get(0) methods: 
System.out.println(mock1.get(0)); //prints 10 
System.out.println(mock2.get(0)); //prints 20 
//using mocks by calling clear() methods: 
mock1.clear(); 
mock2.clear(); 
//verification: 
verify(mock1).clear(); 
verify(mock2).clear(); 
//verifyNoMoreInteractions() fails because get() methods were not accounted for. 
try { verifyNoMoreInteractions(mock1, mock2); } catch (NoInteractionsWanted e); 
//However, if we ignore stubbed methods then we can verifyNoMoreInteractions() 
verifyNoMoreInteractions(ignoreStubs(mock1, mock2)); 
//Remember that ignoreStubs() *changes* the input mocks and returns them for convenience.

Ignoring stubs can be used with verification in order:

List list = mock(List.class); 
when(mock.get(0)).thenReturn("foo"); 
list.add(0); 
System.out.println(list.get(0)); //we don't want to verify this 
list.clear(); 
InOrder inOrder = inOrder(ignoreStubs(list)); 
inOrder.verify(list).add(0); 
inOrder.verify(list).clear(); 
inOrder.verifyNoMoreInteractions();

[中]为了验证,忽略给定模拟的存根方法。有时与verifyNoMoreInteractions()或验证inOrder()结合使用时很有用。有助于避免存根调用的冗余验证——通常我们对验证存根不感兴趣。
警告,ignoreStubs()可能会导致verifyNoMoreInteractions(ignoreStubs(...));的过度使用。请记住,Mockito不建议使用verifyNoMoreInteractions()轰炸每个测试,因为javadoc中列出了Mockito的原因,verifyNoMoreInteractions(Object…)换句话说:给定mock的所有stubbed方法都被标记为verified,这样它们就不会在verifyNoMoreInteractions()期间受到影响。
此方法更改输入模拟!此方法返回输入模拟只是为了方便。
被忽略的存根也将被忽略以进行顺序验证,包括组织。莫基托。为了#验证NoMoreInteractions()。参见第二个示例。
例子:

//mocking lists for the sake of the example (if you mock List in real you will burn in hell) 
List mock1 = mock(List.class), mock2 = mock(List.class); 
//stubbing mocks: 
when(mock1.get(0)).thenReturn(10); 
when(mock2.get(0)).thenReturn(20); 
//using mocks by calling stubbed get(0) methods: 
System.out.println(mock1.get(0)); //prints 10 
System.out.println(mock2.get(0)); //prints 20 
//using mocks by calling clear() methods: 
mock1.clear(); 
mock2.clear(); 
//verification: 
verify(mock1).clear(); 
verify(mock2).clear(); 
//verifyNoMoreInteractions() fails because get() methods were not accounted for. 
try { verifyNoMoreInteractions(mock1, mock2); } catch (NoInteractionsWanted e); 
//However, if we ignore stubbed methods then we can verifyNoMoreInteractions() 
verifyNoMoreInteractions(ignoreStubs(mock1, mock2)); 
//Remember that ignoreStubs() *changes* the input mocks and returns them for convenience.

忽略存根可用于验证,顺序为:

List list = mock(List.class); 
when(mock.get(0)).thenReturn("foo"); 
list.add(0); 
System.out.println(list.get(0)); //we don't want to verify this 
list.clear(); 
InOrder inOrder = inOrder(ignoreStubs(list)); 
inOrder.verify(list).add(0); 
inOrder.verify(list).clear(); 
inOrder.verifyNoMoreInteractions();

代码示例

代码示例来源:origin: info.solidsoft.mockito/mockito-java8

/**
 * Delegates call to {@link Mockito#ignoreStubs(Object...)}.
 */
default Object[] ignoreStubs(Object... mocks) {
  return Mockito.ignoreStubs(mocks);
}

代码示例来源:origin: szpak/mockito-java8

/**
 * Delegates call to {@link Mockito#ignoreStubs(Object...)}.
 */
default Object[] ignoreStubs(Object... mocks) {
  return Mockito.ignoreStubs(mocks);
}

代码示例来源:origin: sniffy/sniffy

@Test
public void testEstimateSendBufferSndBufOptionThrowsException() throws Exception {
  ByteArrayOutputStream expected = new ByteArrayOutputStream();
  SnifferSocketImpl.defaultSendBufferSize = null;
  when(delegate, "getOutputStream").thenReturn(expected);
  when(delegate, "getOption", SO_SNDBUF).thenThrow(new SocketException());
  OutputStream actual = sniffySocket.getOutputStream();
  verifyPrivate(delegate).invoke("getOutputStream");
  verifyNoMoreInteractions(ignoreStubs(delegate));
  assertEquals(SnifferOutputStream.class, actual.getClass());
  actual.write(3);
  assertEquals(3, (int) expected.toByteArray()[0]);
  assertEquals((Integer) 0, SnifferSocketImpl.defaultSendBufferSize);
}

代码示例来源:origin: sniffy/sniffy

@Test
public void testEstimateReceiveBufferRcvBufOptionThrowsException() throws Exception {
  InputStream expected = new ByteArrayInputStream(new byte[]{1,2,3});
  SnifferSocketImpl.defaultReceiveBufferSize = null;
  when(delegate, "getInputStream").thenReturn(expected);
  when(delegate, "getOption", SO_RCVBUF).thenThrow(new SocketException());
  InputStream actual = sniffySocket.getInputStream();
  verifyPrivate(delegate).invoke("getInputStream");
  verifyNoMoreInteractions(ignoreStubs(delegate));
  assertEquals(SnifferInputStream.class, actual.getClass());
  assertEquals(1, actual.read());
  assertEquals((Integer) 0, SnifferSocketImpl.defaultReceiveBufferSize);
}

代码示例来源:origin: sniffy/sniffy

@Test
public void testEstimateSendBufferNoSndBufOption() throws Exception {
  ByteArrayOutputStream expected = new ByteArrayOutputStream();
  SnifferSocketImpl.defaultSendBufferSize = null;
  when(delegate, "getOutputStream").thenReturn(expected);
  when(delegate, "getOption", SO_SNDBUF).thenReturn(null);
  OutputStream actual = sniffySocket.getOutputStream();
  verifyPrivate(delegate).invoke("getOutputStream");
  verifyNoMoreInteractions(ignoreStubs(delegate));
  assertEquals(SnifferOutputStream.class, actual.getClass());
  actual.write(3);
  assertEquals(3, (int) expected.toByteArray()[0]);
  assertEquals((Integer) 0, SnifferSocketImpl.defaultSendBufferSize);
}

代码示例来源:origin: sniffy/sniffy

@Test
public void testEstimateReceiveBufferNoRcvBufOption() throws Exception {
  InputStream expected = new ByteArrayInputStream(new byte[]{1,2,3});
  SnifferSocketImpl.defaultReceiveBufferSize = null;
  when(delegate, "getInputStream").thenReturn(expected);
  when(delegate, "getOption", SO_RCVBUF).thenReturn(null);
  InputStream actual = sniffySocket.getInputStream();
  verifyPrivate(delegate).invoke("getInputStream");
  verifyNoMoreInteractions(ignoreStubs(delegate));
  assertEquals(SnifferInputStream.class, actual.getClass());
  assertEquals(1, actual.read());
  assertEquals((Integer) 0, SnifferSocketImpl.defaultReceiveBufferSize);
}

相关文章