在模拟对象上使用Mockit.reset()

时间:2016-07-23 12:53:38

标签: unit-testing junit mockito

在@Before方法中对模拟对象使用Mockito.reset()是否正确,模拟对象在同一测试类中的测试方法中使用如下所示。

public class SampleTest {   

  @Mock
  private CustomRepository customRepo;

  @Before
  public void setUp() {
    Mockito.reset(customRepo);
  }

  @Test
  public void test1(){
  ......

  given(customRepo.someMethod()).willReturn(Answer1);
  ......
  }

  @Test
  public void test2(){
  ......

  given(customRepo.someMethod()).willReturn(Answer2);
  ......
  }

}

1 个答案:

答案 0 :(得分:1)

您不必重置模拟,因为JUnit / Mockito会为每个测试创建public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png"); public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png"); public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png"); List<Image> images = new ArrayList<>(); try { images.add(ImageIO.read(HelperUi.ICON96)); images.add(ImageIO.read(HelperUi.ICON32)); images.add(ImageIO.read(HelperUi.ICON16)); } catch (IOException e) { LOGGER.error(e, e); } // Define a small and large app icon this.setIconImages(images); 的新实例和模拟对象。