/*
  * Copyright (c) 2007 Mockito contributors
  * This program is made available under the terms of the MIT License.
  */
  org.mockitousage.stubbing;
   
  org.junit.Test;
  org.mockito.Mock;
  org.mockito.invocation.InvocationOnMock;
  org.mockito.stubbing.Answer;
  org.mockitousage.IMethods;
  org.mockitoutil.TestBase;
   
  java.lang.reflect.Method;
  java.util.Set;
   
  org.junit.Assert.*;
  org.mockito.Mockito.*;
   
  TestBase {
  @Mock
  IMethods mock;
   
  @Test
  Exception {
  String>() {
  Throwable {
  0);
   
  + arg;
  }
  });
   
 
  }
   
  @Test
  Exception {
  RecordCall();
  .getMock();
   
  .isEmpty();
   
  .isCalled());
  }
   
  @Test
  Exception {
  .simpleMethod())
  String>() {
  Throwable {
  .getName();
  }
  })
 
  String>() {
  Throwable {
 
  }
  });
   
  .simpleMethod());
  .simpleMethod());
  .simpleMethod());
  .simpleMethod());
  }
   
  @Test
  Exception {
  RecordCall();
   
  .voidMethod();
   
  .voidMethod();
  .isCalled());
  }
   
  @Test
  Exception {
  RecordCall();
  RecordCall();
   
  doAnswer(call1)
  UnsupportedOperationException())
  .doAnswer(call2)
  .voidMethod();
   
  .voidMethod();
  .isCalled());
  .isCalled());
   
  try {
  .voidMethod();
  fail();
  UnsupportedOperationException e) {
  }
   
  .voidMethod();
  .isCalled());
  }
   
  @Test
  Exception {
  String>() {
  Throwable {
  .isArray());
  .getClass());
   
 
  }
  });
   
 
  }
   
  Object> {
  false;
   
  isCalled() {
  return called;
  }
   
  Throwable {
  true;
  null;
  }
  }
   
  }

2.    当mock一个对象,且执行此对象中的方法没有返回值时,使用下面的方法:

import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

类名   对象 = Mockito.mock(类名.class);
        Mockito.doAnswer(new Answer<Object>() {
            public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                return "called with arguments: " + args;
            }
        }).when(对象).方法名();

 

--------------------- 本文来自 flysun3344 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/flysun3344/article/details/52065492?utm_source=copy 

相关文章: