Java Code Examples for org.mockito.Mock

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project arquillian-showcase, under directory /extensions/autodiscover/src/main/java/org/jboss/arquillian/showcase/extension/autodiscover/client/.

Source file: ArchiveProcessor.java

  29 
vote

@Override public void process(Archive<?> applicationArchive,TestClass testClass){
  if (ReflectionHelper.isClassPresent(FEST_ASSERTIONS_CLASS_NAME)) {
    appendFestLibrary(applicationArchive);
  }
  if (ReflectionHelper.isClassPresent(ANNOTATION_CLASS_NAME)) {
    if (ReflectionHelper.getFieldsWithAnnotation(testClass.getJavaClass(),Mock.class).size() > 0) {
      appendMockitoLibrary(applicationArchive);
    }
  }
}
 

Example 2

From project arquillian-showcase, under directory /extensions/autodiscover/src/main/java/org/jboss/arquillian/showcase/extension/autodiscover/container/.

Source file: Enricher.java

  29 
vote

private void enrichFields(Object testCase){
  List<Field> mockFields=ReflectionHelper.getFieldsWithAnnotation(testCase.getClass(),Mock.class);
  for (  Field mockField : mockFields) {
    try {
      Object mocked=Mockito.mock(mockField.getType());
      if (!mockField.isAccessible()) {
        mockField.setAccessible(true);
      }
      mockField.set(testCase,mocked);
    }
 catch (    Exception e) {
      throw new RuntimeException("Could not inject mocked object on field " + mockField,e);
    }
  }
}
 

Example 3

From project com.cedarsoft.serialization, under directory /generator/common/src/test/java/com/cedarsoft/serialization/generator/common/decision/.

Source file: DefaultXmlDecisionCallbackTest.java

  29 
vote

@Test public void testIt() throws Exception {
  new MockitoTemplate(){
    @Mock private FieldInfo fieldInfo;
    @Override protected void stub() throws Exception {
      when(fieldInfo.getSimpleName()).thenReturn("hello").thenReturn("other");
    }
    @Override protected void execute() throws Exception {
      XmlDecisionCallback callback=new DefaultXmlDecisionCallback("other");
      assertEquals(XmlDecisionCallback.Target.ELEMENT,callback.getSerializationTarget(fieldInfo));
      assertEquals(XmlDecisionCallback.Target.ATTRIBUTE,callback.getSerializationTarget(fieldInfo));
    }
    @Override protected void verifyMocks() throws Exception {
      verify(fieldInfo,times(2)).getSimpleName();
      verifyNoMoreInteractions(fieldInfo);
    }
  }
.run();
}
 

Example 4

From project com.cedarsoft.serialization, under directory /generator/common/src/test/java/com/cedarsoft/serialization/generator/common/parsing/.

Source file: ParserTest.java

  29 
vote

@Test public void testVisitor() throws Exception {
  ClassDeclaration classDeclaration=parsed.getClassDeclaration("com.cedarsoft.serialization.generator.common.parsing.test.JavaClassToParse.InnerStaticClass");
  final ImmutableList<FieldDeclaration> fields=ImmutableList.copyOf(classDeclaration.getFields());
  assertEquals("wildStringList",fields.get(1).getSimpleName());
  assertEquals("a",fields.get(2).getSimpleName());
  new MockitoTemplate(){
    @Mock private TypeVisitor visitor;
    @Override protected void stub() throws Exception {
    }
    @Override protected void execute() throws Exception {
      fields.get(1).getType().accept(visitor);
      fields.get(2).getType().accept(visitor);
    }
    @Override protected void verifyMocks() throws Exception {
      verify(visitor).visitInterfaceType(any(InterfaceType.class));
      verify(visitor).visitPrimitiveType(any(PrimitiveType.class));
      verifyNoMoreInteractions(visitor);
    }
  }
.run();
}
 

Example 5

From project com.cedarsoft.serialization, under directory /serialization/src/test/java/com/cedarsoft/serialization/.

Source file: SerializingStrategySupportTest.java

  29 
vote

@Test public void testVersionMappings() throws Exception {
  new MockitoTemplate(){
    @Mock private SerializingStrategy<Integer,StringBuilder,String,IOException> strategy1;
    @Mock private SerializingStrategy<Integer,StringBuilder,String,IOException> strategy2;
    @Override protected void stub() throws Exception {
      when(strategy1.getFormatVersionRange()).thenReturn(VersionRange.single(0,0,1));
      when(strategy2.getFormatVersionRange()).thenReturn(VersionRange.single(0,0,2));
      when(strategy1.getId()).thenReturn("id1");
      when(strategy2.getId()).thenReturn("id2");
    }
    @Override protected void execute() throws Exception {
      support.addStrategy(strategy1).map(1,0,0).toDelegateVersion(0,0,1);
      support.addStrategy(strategy2).map(1,0,0).toDelegateVersion(0,0,2);
      assertTrue(support.verify());
      assertEquals(1,support.getVersionMappings().getMappedVersions().size());
      assertEquals(Version.valueOf(0,0,1),support.resolveVersion(strategy1,Version.valueOf(1,0,0)));
      assertEquals(Version.valueOf(0,0,2),support.resolveVersion(strategy2,Version.valueOf(1,0,0)));
    }
    @Override protected void verifyMocks() throws Exception {
      Mockito.verify(strategy1).getFormatVersionRange();
      Mockito.verifyNoMoreInteractions(strategy1);
      Mockito.verify(strategy2).getFormatVersionRange();
      Mockito.verifyNoMoreInteractions(strategy2);
    }
  }
.run();
}
 

Example 6

From project com.cedarsoft.serialization, under directory /serialization/src/test/java/com/cedarsoft/serialization/.

Source file: SerializingStrategySupportTest.java

  29 
vote

@Test public void festFind3() throws Exception {
  new MockitoTemplate(){
    @Mock private SerializingStrategy<Integer,StringBuilder,String,IOException> strategy;
    @Override protected void stub() throws Exception {
      when(strategy.getFormatVersionRange()).thenReturn(VersionRange.single(1,0,0));
      when(strategy.getId()).thenReturn("daId");
      when(strategy.supports(77)).thenReturn(true);
      when(strategy.supports(99)).thenReturn(false);
    }
    @Override protected void execute() throws Exception {
      assertNotNull(strategy);
      assertEquals(0,support.getStrategies().size());
      support.addStrategy(strategy);
      assertEquals(1,support.getStrategies().size());
      assertNotNull(support.findStrategy("daId"));
      try {
        support.findStrategy("otherId");
        fail("Where is the Exception");
      }
 catch (      NotFoundException ignore) {
      }
      assertNotNull(support.findStrategy(77));
      try {
        support.findStrategy(99);
        fail("Where is the Exception");
      }
 catch (      NotFoundException ignore) {
      }
    }
    @Override protected void verifyMocks() throws Exception {
      verify(strategy).getFormatVersionRange();
      verify(strategy,times(2)).getId();
      verify(strategy).supports(77);
      verify(strategy).supports(99);
      verifyNoMoreInteractions(strategy);
    }
  }
.run();
}
 

Example 7

From project JDave, under directory /jdave-guice/src/main/java/jdave/guice/internal/.

Source file: GuiceAnnotationEngine.java

  29 
vote

public Object createMockFor(final Annotation annotation,final Field field){
  if (annotation instanceof Mock) {
    return Mockito.mock(field.getType(),field.getName());
  }
  if (annotation instanceof GuiceMock) {
    return bindMock(field.getType(),field);
  }
  return null;
}