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

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

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

Mockito.validateMockitoUsage介绍

[英]First of all, in case of any trouble, I encourage you to read the Mockito FAQ: http://code.google.com/p/mockito/wiki/FAQ

In case of questions you may also post to mockito mailing list: http://groups.google.com/group/mockito

validateMockitoUsage()explicitly validates the framework state to detect invalid use of Mockito. However, this feature is optional because Mockito validates the usage all the time... but there is a gotcha so read on.

Examples of incorrect use:

//Oups, someone forgot thenReturn() part: 
when(mock.get()); 
//Oups, someone put the verified method call inside verify() where it should be outside: 
verify(mock.execute()); 
//Oups, someone has used EasyMock for too long and forgot to specify the method to verify: 
verify(mock);

Mockito throws exceptions if you misuse it so that you know if your tests are written correctly. The gotcha is that Mockito does the validation next time you use the framework (e.g. next time you verify, stub, call mock etc.). But even though the exception might be thrown in the next test, the exception message contains a navigable stack trace element with location of the defect. Hence you can click and find the place where Mockito was misused.

Sometimes though, you might want to validate the framework usage explicitly. For example, one of the users wanted to put validateMockitoUsage() in his @After method so that he knows immediately when he misused Mockito. Without it, he would have known about it not sooner than next time he used the framework. One more benefit of having validateMockitoUsage() in @After is that jUnit runner will always fail in the test method with defect whereas ordinary 'next-time' validation might fail the next test method. But even though JUnit might report next test as red, don't worry about it and just click at navigable stack trace element in the exception message to instantly locate the place where you misused mockito.

Built-in runner: MockitoJUnitRunner does validateMockitoUsage() after each test method.

Bear in mind that usually you don't have to validateMockitoUsage() and framework validation triggered on next-time basis should be just enough, mainly because of enhanced exception message with clickable location of defect. However, I would recommend validateMockitoUsage() if you already have sufficient test infrastructure (like your own runner or base class for all tests) because adding a special action to @After has zero cost.

See examples in javadoc for Mockito class
[中]

代码示例

代码示例来源:origin: fabioCollini/DaggerMock

@Override
  public void evaluate() throws Throwable {
    MockitoAnnotations.initMocks(target);
    setupComponent(target);
    base.evaluate();
    Mockito.validateMockitoUsage();
  }
};

代码示例来源:origin: fabioCollini/DaggerMock

public void initMocks(Object target) {
  MockitoAnnotations.initMocks(target);
  setupComponent(target);
  Mockito.validateMockitoUsage();
}

代码示例来源:origin: apache/flink

@After
public void validate() {
  validateMockitoUsage();
}

代码示例来源:origin: google/j2objc

@Override
  public void testFinished(Description description) throws Exception {
    super.testFinished(description);
    try {
      Mockito.validateMockitoUsage();
    } catch(Throwable t) {
      notifier.fireTestFailure(new Failure(description, t));
    }
  }
}

代码示例来源:origin: org.mockito/mockito-core

@Override
  public void finishMocking(final Throwable failure) {
    //Cleaning up the state, we no longer need the listener hooked up
    //The listener implements MockCreationListener and at this point
    //we no longer need to listen on mock creation events. We are wrapping up the session
    Mockito.framework().removeListener(listener);

    //Emit test finished event so that validation such as strict stubbing can take place
    listener.testFinished(new TestFinishedEvent() {
      @Override
      public Throwable getFailure() {
        return failure;
      }
      @Override
      public String getTestName() {
        return name;
      }
    });

    //Validate only when there is no test failure to avoid reporting multiple problems
    if (failure == null) {
      //Finally, validate user's misuse of Mockito framework.
      Mockito.validateMockitoUsage();
    }
  }
}

代码示例来源:origin: facebook/litho

@After
public void validate() {
 validateMockitoUsage();
}

代码示例来源:origin: ankidroid/Anki-Android

@After
public void validate() {
  Mockito.validateMockitoUsage();
}

代码示例来源:origin: spring-projects/spring-security

@After
public void cleanup() throws Exception {
  validateMockitoUsage();
  this.server.shutdown();
}

代码示例来源:origin: iSoron/uhabits

@After
public void tearDown() throws Exception
{
  validateMockitoUsage();
  DateUtils.setFixedLocalTime(null);
}

代码示例来源:origin: facebook/litho

assertThat(mountBounds).isEqualTo(new Rect(20, 0, 280, 0));
validateMockitoUsage();

代码示例来源:origin: facebook/litho

assertThat(mountBounds).isEqualTo(new Rect(20, 0, 280, 0));
validateMockitoUsage();

代码示例来源:origin: facebook/litho

assertThat(mountBounds).isEqualTo(new Rect(20, 0, 280, 0));
validateMockitoUsage();

代码示例来源:origin: facebook/litho

assertThat(mountBounds).isEqualTo(new Rect(28, 8, 272, 8));
validateMockitoUsage();

代码示例来源:origin: com.atlassian.jira/jira-tests

@Override
  protected void finished(Description description)
  {
    Mockito.validateMockitoUsage();
  }
}

代码示例来源:origin: net.sf.twip/twip

@Override
  public void evaluate() throws Throwable {
    statement.evaluate();
    Mockito.validateMockitoUsage();
  }
}

代码示例来源:origin: org.atteo.moonshine/container-test-utils

@Override
  public void evaluate() throws Throwable {
    resetMocks();
    base.evaluate();
    Mockito.validateMockitoUsage();
  }
};

代码示例来源:origin: name.didier.david/ndd-test4j

/**
 * Validate Mockito usage and reset mocks.
 *
 * @param testMethod the invoked test method.
 * @param testResult metadata about the class under test.
 */
public void applyFor(IInvokedMethod testMethod, ITestResult testResult) {
  Mockito.validateMockitoUsage();
  if (testMethod.isTestMethod()) {
    Object testInstance = testResult.getInstance();
    Mockito.reset(instanceMocksOf(testInstance).toArray());
  }
}

代码示例来源:origin: mesosphere/dcos-commons

@After
public void afterTest() {
 Mockito.validateMockitoUsage();
}

代码示例来源:origin: ArcBees/Jukito

@Override
  public void testFinished(org.junit.runner.Description description) throws Exception {
    super.testFinished(description);

    try {
      Mockito.validateMockitoUsage();
    } catch (Throwable t) {
      notifier.fireTestFailure(new Failure(description, t));
    }
  }
}

代码示例来源:origin: locationtech/geogig

/**
 * Validate mockito usage right after a test case so it doesn't reports bad usage on the next
 * test case which is misleading
 */
@After
public void validate() {
  validateMockitoUsage();
}

相关文章

微信公众号

最新文章

更多