當前位置: 首頁>>代碼示例>>Java>>正文


Java Mockito.mock方法代碼示例

本文整理匯總了Java中org.mockito.Mockito.mock方法的典型用法代碼示例。如果您正苦於以下問題:Java Mockito.mock方法的具體用法?Java Mockito.mock怎麽用?Java Mockito.mock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.mockito.Mockito的用法示例。


在下文中一共展示了Mockito.mock方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testMissingHostname

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void testMissingHostname() throws Exception {
  ServletRequest request = Mockito.mock(ServletRequest.class);
  Mockito.when(request.getRemoteAddr()).thenReturn(null);

  ServletResponse response = Mockito.mock(ServletResponse.class);

  final AtomicBoolean invoked = new AtomicBoolean();

  FilterChain chain = new FilterChain() {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
      throws IOException, ServletException {
      assertTrue(HostnameFilter.get().contains("???"));
      invoked.set(true);
    }
  };

  Filter filter = new HostnameFilter();
  filter.init(null);
  assertNull(HostnameFilter.get());
  filter.doFilter(request, response, chain);
  assertTrue(invoked.get());
  assertNull(HostnameFilter.get());
  filter.destroy();
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:27,代碼來源:TestHostnameFilter.java

示例2: testSubscribe

import org.mockito.Mockito; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
@Test
public void testSubscribe() throws Exception {
    Consumer<Integer> callback = Mockito.mock(Consumer.class);
    Consumer<Integer> callback2 = Mockito.mock(Consumer.class);
    store = new RxStore<>(initialState, this::reducer);

    store.subscribe(callback);
    verify(callback, Mockito.never()).accept(any());
    verify(callback2, Mockito.never()).accept(any());

    store.dispatch(INC);
    verify(callback).accept(initialState + 1);
    verify(callback2, Mockito.never()).accept(any());

    store.subscribe(callback2);
    store.dispatch(INC);
    verify(callback).accept(initialState + 2);
    verify(callback2).accept(initialState + 2);

    store.dispatch(DEC);
    verify(callback, times(2)).accept(initialState + 1);
    verify(callback2).accept(initialState + 1);

    assertEquals("The state at the end should not be influenced by the subscriptions", initialState + 1, store.getState().intValue());
}
 
開發者ID:Martori,項目名稱:rx-redux,代碼行數:27,代碼來源:RxStoreTest.java

示例3: returns_compilation_result_when_build_success

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void returns_compilation_result_when_build_success() throws IOException {
    final String workspace = folder.newFolder().getAbsolutePath();
    final Path workspacePath = Paths.get(workspace);
    final String targetDirectory = Paths.get(workspace, "InvalidClassUnderCompilationTest.java").toString();

    final InternalCompiler compiler = Mockito.mock(InternalCompiler.class);
    Mockito.when(compiler.compile(any(), any(), any()))
           .thenReturn(CompilationResult.compilationSucceeded(workspacePath));

    this.compiler.init(workspace, Mockito.mock(DependencyResolver.class), compiler);

    final String javaFile = ResourceUtils.getFileFromClassPath("java/InvalidClassUnderCompilationTest.java");
    final CompilationResult result = this.compiler.compile(javaFile, null);

    assertTrue(result.isSuccess());
    assertEquals(workspacePath, result.getPath());
    Mockito.verify(compiler).compile(new File(javaFile), targetDirectory, Arrays.asList("-d", targetDirectory));
}
 
開發者ID:dshell-io,項目名稱:dshell,代碼行數:20,代碼來源:DefaultJavaFileCompilerTest.java

示例4: testValidationNegativeCustomPattern

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void testValidationNegativeCustomPattern() throws Exception {

    PropertyHandler ph = Mockito.mock(PropertyHandler.class);
    Mockito.when(ph.getStackName()).thenReturn("Test123");
    Mockito.when(ph.getStackNamePattern()).thenReturn("Test");

    try {
        validateStackName(ph);
        fail();
    } catch (APPlatformException e) {
    }
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:14,代碼來源:ProvisioningValidatorTest.java

示例5: whenVoidMethodNoArgsButWithParamsInSignatureThenThrowsException

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void whenVoidMethodNoArgsButWithParamsInSignatureThenThrowsException() throws Exception {
    expectedEx.expect(IllegalStateException.class);
    expectedEx.expectMessage(Matchers.startsWith("Call proceed without parameters on method"));

    MethodSignature signature = Mockito.mock(MethodSignature.class);
    when(signature.getParameterTypes()).thenReturn(new Class<?>[] {String.class});

    LepMethod method = Mockito.mock(LepMethod.class);
    when(method.getMethodSignature()).thenReturn(signature);

    TargetProceedingLep proceedingLep = new TargetProceedingLep(method, null);
    proceedingLep.proceed();
}
 
開發者ID:xm-online,項目名稱:xm-commons,代碼行數:15,代碼來源:TargetProceedingLepUnitTest.java

示例6: testInitialize

import org.mockito.Mockito; //導入方法依賴的package包/類
@Override
@Before
public void testInitialize() throws Exception{
    super.testInitialize();

    ApiFactoryService apiFactoryService = Mockito.mock(ApiFactoryService.class);
    Mockito.when(apiFactoryService.syncsPolicyMapping("NSM")).thenReturn(true);
}
 
開發者ID:opensecuritycontroller,項目名稱:osc-core,代碼行數:9,代碼來源:DistributedApplianceDtoValidatorTest.java

示例7: mockClassLoaderCache

import org.mockito.Mockito; //導入方法依賴的package包/類
static ClassLoaderCache mockClassLoaderCache() throws NoSuchFieldException, IllegalAccessException {
    ClassLoaderCache mockedClassLoaderCache = Mockito.mock(ClassLoaderCache.class);
    Mockito.when(mockedClassLoaderCache.currentClassLoader()).thenReturn(Thread.currentThread().getContextClassLoader());
    Field instance = ClassLoaderCache.class.getDeclaredField("instance");
    instance.setAccessible(true);
    instance.set(null, mockedClassLoaderCache);
    return mockedClassLoaderCache;
}
 
開發者ID:fstab,項目名稱:promagent,代碼行數:9,代碼來源:Util.java

示例8: setUp

import org.mockito.Mockito; //導入方法依賴的package包/類
@Before
public void setUp() throws JSONException, BlockLoadingException {
    mMockController = Mockito.mock(BlocklyController.class);

    factory = new BlockFactory();
    factory.setController(mMockController);
    factory.addDefinition(new BlockDefinition("{\"type\": \"dummyBlock\"}"));
    dummyBlock = new BlockTemplate().ofType("dummyBlock");
    shadowBlock = new BlockTemplate(dummyBlock).shadow();

    input = new Connection(Connection.CONNECTION_TYPE_INPUT, null);
    input.setBlock(factory.obtainBlockFrom(dummyBlock));

    output = new Connection(CONNECTION_TYPE_OUTPUT, null);
    output.setBlock(factory.obtainBlockFrom(dummyBlock));

    previous = new Connection(Connection.CONNECTION_TYPE_PREVIOUS, null);
    previous.setBlock(factory.obtainBlockFrom(dummyBlock));

    next = new Connection(Connection.CONNECTION_TYPE_NEXT, null);
    next.setBlock(factory.obtainBlockFrom(dummyBlock));

    shadowInput = new Connection(Connection.CONNECTION_TYPE_INPUT, null);
    shadowInput.setBlock(factory.obtainBlockFrom(shadowBlock));

    shadowOutput = new Connection(CONNECTION_TYPE_OUTPUT, null);
    shadowOutput.setBlock(factory.obtainBlockFrom(shadowBlock));

    shadowPrevious = new Connection(Connection.CONNECTION_TYPE_PREVIOUS, null);
    shadowPrevious.setBlock(factory.obtainBlockFrom(shadowBlock));

    shadowNext = new Connection(Connection.CONNECTION_TYPE_NEXT, null);
    shadowNext.setBlock(factory.obtainBlockFrom(shadowBlock));
}
 
開發者ID:Axe-Ishmael,項目名稱:Blockly,代碼行數:35,代碼來源:ConnectionTest.java

示例9: createInvoice

import org.mockito.Mockito; //導入方法依賴的package包/類
public static Invoice createInvoice(DateTime createdDate, LocalDate invoiceDate,
        List<InvoiceItem> invoiceItems) {
    UUID id = UUID.randomUUID();
    Invoice invoice = Mockito.mock(Invoice.class,
            Mockito.withSettings().defaultAnswer(RETURNS_SMART_NULLS.get()));
    when(invoice.getId()).thenReturn(id);
    when(invoice.getCreatedDate()).thenReturn(createdDate);
    when(invoice.getInvoiceDate()).thenReturn(invoiceDate);
    when(invoice.getInvoiceItems()).thenReturn(invoiceItems);
    return invoice;
}
 
開發者ID:SolarNetwork,項目名稱:killbill-easytax-plugin,代碼行數:12,代碼來源:EasyTaxTestUtils.java

示例10: testBUG401Mockito

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
@Category(value=UnitTest.class)
public void testBUG401Mockito() {
	Random mock = Mockito.mock(Random.class);

	// rehearse
	when(mock.nextInt(7)).thenReturn(5);

	// run test
	Die die = new JavaRandomDie(mock);
	Die copyDie = die.roll();
	assertThat(copyDie.getPips()).isGreaterThan(0).isLessThan(7);
}
 
開發者ID:dhinojosa,項目名稱:tddinjava_2017-08-14,代碼行數:14,代碼來源:JavaRandomDieTest.java

示例11: testMessageMapperIsEmptyByDefaultAndSupportsMapping

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void testMessageMapperIsEmptyByDefaultAndSupportsMapping() {
	SingleModelMessageMapper<Integer> singleModelMessageMapper =
		() -> "mediaType";

	HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);

	SingleModel<Integer> singleModel = new SingleModel<>(3, Integer.class);

	assertThat(
		singleModelMessageMapper.supports(singleModel, httpHeaders),
		is(true));
}
 
開發者ID:liferay,項目名稱:com-liferay-apio-architect,代碼行數:14,代碼來源:SingleModelMessageMapperTest.java

示例12: getElement

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void getElement() {
    RskCustomCache cache = new RskCustomCache(TIME_TO_LIVE);

    BlockHeader header1 = Mockito.mock(BlockHeader.class);
    cache.put(KEY, header1);

    Assert.assertNotNull(cache.get(KEY));
    Assert.assertNull(cache.get(OTHER_KEY));
}
 
開發者ID:rsksmart,項目名稱:rskj,代碼行數:11,代碼來源:RskCustomCacheTest.java

示例13: checkForwardTo

import org.mockito.Mockito; //導入方法依賴的package包/類
/**
 * Test use case forward.
 */
private void checkForwardTo(final String from, final String to, final String suffix) throws IOException, ServletException {
	final HtmlProxyFilter htmlProxyFilter = new HtmlProxyFilter();
	htmlProxyFilter.setSuffix(suffix);

	final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
	final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
	Mockito.when(request.getServletPath()).thenReturn(from);
	final RequestDispatcher requestDispatcher = Mockito.mock(RequestDispatcher.class);
	Mockito.when(request.getRequestDispatcher(to)).thenReturn(requestDispatcher);
	htmlProxyFilter.doFilter(request, response, null);
	Mockito.verify(requestDispatcher, Mockito.atLeastOnce()).forward(request, response);
	Mockito.validateMockitoUsage();
}
 
開發者ID:ligoj,項目名稱:bootstrap,代碼行數:17,代碼來源:HtmlProxyFilterTest.java

示例14: testInitSpring

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void testInitSpring() {
  boolean status = true;
  RestServletContextListener listener = new RestServletContextListener();
  ServletContextEvent sce = Mockito.mock(ServletContextEvent.class);
  ServletContext context = Mockito.mock(ServletContext.class);
  Mockito.when(sce.getServletContext()).thenReturn(context);
  Mockito.when(sce.getServletContext().getInitParameter("contextConfigLocation")).thenReturn("locations");
  try {
    listener.initSpring(sce);
  } catch (Exception e) {
    status = false;
  }
  Assert.assertFalse(status);
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:16,代碼來源:TestRestServletContextListener.java

示例15: onResponseContent

import org.mockito.Mockito; //導入方法依賴的package包/類
@Test
public void onResponseContent() throws IOException {
	final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
	final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
	final ServletOutputStream outputStream = Mockito.mock(ServletOutputStream.class);
	Mockito.when(response.getOutputStream()).thenReturn(outputStream);
	final Response proxyResponse = Mockito.mock(Response.class);
	servlet.onResponseContent(new HttpServletRequestWrapper(request), response, proxyResponse, null, 0, 0, callback);
	Mockito.verify(outputStream, Mockito.times(1)).write(null, 0, 0);

}
 
開發者ID:ligoj,項目名稱:bootstrap,代碼行數:12,代碼來源:BackendProxyServletTest.java


注:本文中的org.mockito.Mockito.mock方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。