Java Code Examples for org.mockito.Mockito#verify()

The following examples show how to use org.mockito.Mockito#verify() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Wrapper around {@link Mockito#verify}, to keep log spam down on failure.
 */
private static <T> T verify(T mock, VerificationMode mode) {
  try {
    return Mockito.verify(mock, mode);
  } catch (final AssertionError e) {
    String msg = e.getMessage();
    if (msg.length() >= 256) {
      // AssertionError(String, Throwable) only present in Android API 19+
      throw new AssertionError(msg.substring(0, 256)) {
        @Override
        public synchronized Throwable getCause() {
          return e;
        }
      };
    }
    throw e;
  }
}
 
Example 2
/**
 * Test method for
 * {@link VarStrategy#execute(org.jvoicexml.interpreter.VoiceXmlInterpreterContext, org.jvoicexml.interpreter.VoiceXmlInterpreter, org.jvoicexml.interpreter.FormInterpretationAlgorithm, org.jvoicexml.interpreter.FormItem, org.jvoicexml.xml.VoiceXmlNode)}
 * .
 * 
 * @exception Exception
 *                Test failed.
 */
@Test
public void testExecuteUndefined() throws Exception {
    final String name = "test";
    final Block block = createBlock();
    final Var var = block.appendChild(Var.class);
    var.setName(name);

    final VarStrategy strategy = new VarStrategy();
    try {
        executeTagStrategy(var, strategy);
    } catch (JVoiceXMLEvent e) {
        Assert.fail(e.getMessage());
    }

    final DataModel model = getDataModel();
    Mockito.verify(model.createVariable(name));
}
 
Example 3
/**
 *  Success case - in case of 200
 * @throws Exception
 */
@Test
public void testDecodeHttpResponse() throws Exception {
    HttpResponse mockHttpResponse = mock(HttpResponse.class);
    StatusLine mockStatusLine = mock(StatusLine.class);
    HttpEntity mockEntity = mock(HttpEntity.class);
    InputStream stream = mock(InputStream.class);
    mockStatic(IOUtils.class);

    when(mockStatusLine.getStatusCode()).thenReturn(200);
    when(mockHttpResponse.getStatusLine()).thenReturn(mockStatusLine);
    when(mockHttpResponse.getEntity()).thenReturn(mockEntity);
    when(mockEntity.getContent()).thenReturn(stream);
    BDDMockito.when(IOUtils.toString(stream)).thenReturn("success");
    when(mockJavaType.getRawClass()).thenReturn(responseClass);

    ServiceResponseDecoder decoder = spy(new ServiceResponseDecoder(mockMapper, mockJavaType, mockErrorType, mockLogger , exceptions, collectedHeaders));
    ServiceResponse response = decoder.decode(mockHttpResponse);
    assertEquals("success", response.getDataList().get(0));
    Mockito.verify(mockLogger, Mockito.never());
}
 
Example 4
Source Project: ranger   File: TestXUserREST.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test50getXAuditMapVXAuditMapNull() {
	VXAuditMap testvXAuditMap =  createVXAuditMapObj();
	Mockito.when(xUserMgr.getXAuditMap(testvXAuditMap.getResourceId())).thenReturn(testvXAuditMap);

	Mockito.when(restErrorUtil.createRESTException(Mockito.anyString(), (MessageEnums)Mockito.any())).thenThrow(new WebApplicationException());
	thrown.expect(WebApplicationException.class);
	
	VXAuditMap retVXAuditMap=xUserRest.getXAuditMap(testvXAuditMap.getResourceId());
	
	assertEquals(testvXAuditMap.getId(),retVXAuditMap.getId());
	assertEquals(testvXAuditMap.getClass(),retVXAuditMap.getClass());
	assertNotNull(retVXAuditMap);
	
	Mockito.verify(xUserMgr).getXAuditMap(testvXAuditMap.getResourceId());
	Mockito.verify(xResourceService).readResource(null);
	Mockito.verify(restErrorUtil.createRESTException(Mockito.anyString(), (MessageEnums)Mockito.any()));
	
}
 
Example 5
Source Project: ranger   File: TestXUserREST.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test54createXAuditMapVxResourceNull() {

	VXAuditMap testvXAuditMap =  createVXAuditMapObj();
	testvXAuditMap.setResourceId(null);
	
	Mockito.when(xResourceService.readResource(testvXAuditMap.getResourceId())).thenReturn(null);

	Mockito.when(restErrorUtil.createRESTException(Mockito.anyString(), (MessageEnums)Mockito.any())).thenThrow(new WebApplicationException());
	thrown.expect(WebApplicationException.class);
	
	VXAuditMap retvXAuditMap= xUserRest.createXAuditMap(testvXAuditMap);
	assertEquals(testvXAuditMap.getId(),retvXAuditMap.getId());
	assertEquals(testvXAuditMap.getClass(),retvXAuditMap.getClass());
	assertNotNull(retvXAuditMap);
	
	Mockito.verify(xUserMgr).createXAuditMap(testvXAuditMap);
	Mockito.verify(xResourceService).readResource(testvXAuditMap.getResourceId());
	Mockito.verify(restErrorUtil.createRESTException(Mockito.anyString(), (MessageEnums)Mockito.any()));
	
}
 
Example 6
@Test
public void testConstructor() throws Exception {

    ServiceReferenceHolderMockCreator holderMockCreator = new ServiceReferenceHolderMockCreator(1);
    ServiceReferenceHolderMockCreator.initContextService();
    holderMockCreator.initRegistryServiceMockCreator(false, new Object());
    RegistryAuthorizationManager registryAuthorizationManager = Mockito.mock(RegistryAuthorizationManager.class);
    Mockito.doThrow(UserStoreException.class).doNothing().when(registryAuthorizationManager)
            .authorizeRole(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
    PowerMockito.whenNew(RegistryAuthorizationManager.class).withAnyArguments()
            .thenReturn(registryAuthorizationManager);
    PowerMockito.mockStatic(RegistryUtils.class);
    PowerMockito.when(RegistryUtils.getAbsolutePath((RegistryContext) Mockito.any(), Mockito.anyString()))
            .thenReturn("/test");
    try {
        new AbstractAPIManager(null) {
            @Override
            public String getGraphqlSchema(APIIdentifier apiId) throws APIManagementException {
                return null;
            }
        };
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while setting the permissions"));
    }

    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.doNothing().when(APIUtil.class, "loadTenantRegistry", Mockito.anyInt());
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn(SAMPLE_TENANT_DOMAIN_1);
    String userName = "admin";

    Mockito.verify(
            holderMockCreator.getRegistryServiceMockCreator().getMock().getConfigSystemRegistry(Mockito.anyInt()),
            Mockito.atLeastOnce());
}
 
Example 7
@Test
public void toMap() {
    // Given
    NSDictionary nsDictionary = Mockito.mock(NSDictionary.class);
    Set set = Mockito.spy(new HashSet());
    Mockito.when(nsDictionary.keySet()).thenReturn(set);

    // When
    Object result = NSDictionaryHelper.toMap(nsDictionary);

    // Then
    Assert.assertNotNull(result);
    Mockito.verify(nsDictionary.keySet(), VerificationModeFactory.times(1));
}
 
Example 8
Source Project: claudb   File: CommandRule.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T verify(Class<T> type) {
  if (type.equals(ServerContext.class)) {
    return (T) Mockito.verify(server);
  } else if (type.equals(DBServerContext.class)) {
    return (T) Mockito.verify(server);
  } else if (type.equals(Session.class)) {
    return (T) Mockito.verify(session);
  }
  throw new IllegalArgumentException();
}
 
Example 9
@Test
public void testHeaders() throws Exception {
    HttpResponse mockHttpResponse = mock(HttpResponse.class);
    StatusLine mockStatusLine = mock(StatusLine.class);
    HttpEntity mockEntity = mock(HttpEntity.class);
    InputStream stream = mock(InputStream.class);
    mockStatic(IOUtils.class);

    Header headerOne = new BasicHeader("one", "1");
    Header headerTwo = new BasicHeader("two", "2");
    Header[] responseHeaders = new Header[] { headerOne, headerTwo };

    when(mockStatusLine.getStatusCode()).thenReturn(200);
    when(mockHttpResponse.getStatusLine()).thenReturn(mockStatusLine);
    when(mockHttpResponse.getEntity()).thenReturn(mockEntity);
    when(mockHttpResponse.getAllHeaders()).thenReturn(responseHeaders);
    when(mockEntity.getContent()).thenReturn(stream);
    BDDMockito.when(IOUtils.toString(stream)).thenReturn("success");
    when(mockJavaType.getRawClass()).thenReturn(responseClass);

    ServiceResponseDecoder decoder = spy(new ServiceResponseDecoder(mockMapper, mockJavaType, mockErrorType, mockLogger , exceptions, collectedHeaders));
    ServiceResponse response = decoder.decode(mockHttpResponse);
    assertEquals("success", response.getDataList().get(0));
    assertEquals(2, response.getHeaders().size());
    assertEquals("1", response.getHeaders().get("one"));
    assertEquals("2", response.getHeaders().get("two"));
    assertEquals(0, collectedHeaders.size());
    Mockito.verify(mockLogger, Mockito.never());
}
 
Example 10
@Test
public void testCollectedHeaders() throws Exception {
    HttpResponse mockHttpResponse = mock(HttpResponse.class);
    StatusLine mockStatusLine = mock(StatusLine.class);
    HttpEntity mockEntity = mock(HttpEntity.class);
    InputStream stream = mock(InputStream.class);
    mockStatic(IOUtils.class);

    collectedHeaders.put("one", new ConcurrentLinkedQueue<>());

    Header headerOne = new BasicHeader("one", "1");
    Header headerOneAgain = new BasicHeader("one", "3");
    Header headerTwo = new BasicHeader("two", "2");
    Header[] responseHeaders = new Header[] { headerOne, headerTwo, headerOneAgain };

    when(mockStatusLine.getStatusCode()).thenReturn(200);
    when(mockHttpResponse.getStatusLine()).thenReturn(mockStatusLine);
    when(mockHttpResponse.getEntity()).thenReturn(mockEntity);
    when(mockHttpResponse.getAllHeaders()).thenReturn(responseHeaders);
    when(mockEntity.getContent()).thenReturn(stream);
    BDDMockito.when(IOUtils.toString(stream)).thenReturn("success");
    when(mockJavaType.getRawClass()).thenReturn(responseClass);

    ServiceResponseDecoder decoder = spy(new ServiceResponseDecoder(mockMapper, mockJavaType, mockErrorType, mockLogger , exceptions, collectedHeaders));
    ServiceResponse response = decoder.decode(mockHttpResponse);
    assertEquals("success", response.getDataList().get(0));
    assertEquals(2, response.getHeaders().size());
    assertEquals("3", response.getHeaders().get("one"));
    assertEquals("2", response.getHeaders().get("two"));
    assertEquals(1, collectedHeaders.size());
    assertEquals(2, collectedHeaders.get("one").size());
    assertTrue(collectedHeaders.get("one").contains("1"));
    assertTrue(collectedHeaders.get("one").contains("3"));
    Mockito.verify(mockLogger, Mockito.never());

}
 
Example 11
Source Project: resp-server   File: CommandRule.java    License: MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T verify(Class<T> type) {
  if (type.equals(ServerContext.class)) {
    return (T) Mockito.verify(server);
  } else if (type.equals(Session.class)) {
    return (T) Mockito.verify(session);
  }
  throw new IllegalArgumentException();
}
 
Example 12
Source Project: ranger   File: TestXUserREST.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test57updateXAuditMapNull() {
	VXAuditMap testvXAuditMap =  createVXAuditMapObj();
	
	
	Mockito.when(restErrorUtil.createRESTException(Mockito.anyString(), (MessageEnums)Mockito.any())).thenThrow(new WebApplicationException());
	thrown.expect(WebApplicationException.class);
	VXAuditMap retvXAuditMap=xUserRest.updateXAuditMap(testvXAuditMap);
	assertNull(retvXAuditMap);
	Mockito.verify(xUserMgr).updateXAuditMap(testvXAuditMap);
	Mockito.verify(xResourceService).readResource(null);
	Mockito.verify(restErrorUtil.createRESTException(Mockito.anyString(), (MessageEnums)Mockito.any()));
}
 
Example 13
Source Project: quarkus   File: PanacheMock.java    License: Apache License 2.0 4 votes vote down vote up
public static <T> T verify(Class<T> klass) {
    return Mockito.verify(getMock(klass));
}
 
Example 14
protected EventListener verify(int index) {
    return Mockito.verify(listener(index));
}
 
Example 15
Source Project: Patterdale   File: WithMockito.java    License: Apache License 2.0 4 votes vote down vote up
default <T> T verify(T mock) {
    return Mockito.verify(mock);
}
 
Example 16
Source Project: rapidoid   File: TestCommons.java    License: Apache License 2.0 4 votes vote down vote up
protected <T> T verify(T mock) {
	return Mockito.verify(mock);
}
 
Example 17
Source Project: mocker   File: Mocker.java    License: Apache License 2.0 4 votes vote down vote up
public T never(Action1<T> verify) {
    T mock = mock();
    verify.call(mock);
    return Mockito.verify(mock, Mockito.never());
}
 
Example 18
Source Project: mocker   File: Mocker.java    License: Apache License 2.0 4 votes vote down vote up
public T times(Action1<T> verify, int i) {
    T mock = mock();
    verify.call(mock);
    return Mockito.verify(mock, Mockito.times(i));
}
 
Example 19
Source Project: mockito-java8   File: WithMockito.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Delegates call to {@link Mockito#verify(Object)}.
 */
default <T> T verify(T mock) {
    return Mockito.verify(mock, times(1));
}
 
Example 20
Source Project: mockito-java8   File: WithMockito.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Delegates call to {@link Mockito#verify(Object, VerificationMode)}.
 */
default <T> T verify(T mock, VerificationMode mode) {
    return Mockito.verify(mock, mode);
}