1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.util; 6 7 import org.mockito.MockingDetails; 8 import org.mockito.exceptions.misusing.NotAMockException; 9 import org.mockito.internal.debugging.InvocationsPrinter; 10 import org.mockito.internal.stubbing.InvocationContainerImpl; 11 import org.mockito.invocation.Invocation; 12 import org.mockito.invocation.MockHandler; 13 import org.mockito.mock.MockCreationSettings; 14 import org.mockito.stubbing.Stubbing; 15 16 import java.util.Collection; 17 18 /** 19 * Class to inspect any object, and identify whether a particular object is either a mock or a spy. This is 20 * a wrapper for {@link org.mockito.internal.util.MockUtil}. 21 */ 22 public class DefaultMockingDetails implements MockingDetails { 23 24 private final Object toInspect; 25 DefaultMockingDetails(Object toInspect)26 public DefaultMockingDetails(Object toInspect){ 27 this.toInspect = toInspect; 28 } 29 30 @Override isMock()31 public boolean isMock(){ 32 return MockUtil.isMock(toInspect); 33 } 34 35 @Override isSpy()36 public boolean isSpy(){ 37 return MockUtil.isSpy(toInspect); 38 } 39 40 @Override getInvocations()41 public Collection<Invocation> getInvocations() { 42 return getInvocationContainer().getInvocations(); 43 } 44 getInvocationContainer()45 private InvocationContainerImpl getInvocationContainer() { 46 assertGoodMock(); 47 return MockUtil.getInvocationContainer(toInspect); 48 } 49 50 @Override getMockCreationSettings()51 public MockCreationSettings<?> getMockCreationSettings() { 52 return mockHandler().getMockSettings(); 53 } 54 55 @Override getStubbings()56 public Collection<Stubbing> getStubbings() { 57 return getInvocationContainer().getStubbingsAscending(); 58 } 59 60 @Override printInvocations()61 public String printInvocations() { 62 assertGoodMock(); 63 return new InvocationsPrinter().printInvocations(toInspect); 64 } 65 66 @Override getMockHandler()67 public MockHandler getMockHandler() { 68 return mockHandler(); 69 } 70 71 @Override getMock()72 public Object getMock() { 73 return toInspect; 74 } 75 mockHandler()76 private MockHandler<Object> mockHandler() { 77 assertGoodMock(); 78 return MockUtil.getMockHandler(toInspect); 79 } 80 assertGoodMock()81 private void assertGoodMock() { 82 if (toInspect == null) { 83 throw new NotAMockException("Argument passed to Mockito.mockingDetails() should be a mock, but is null!"); 84 } else if (!isMock()) { 85 throw new NotAMockException("Argument passed to Mockito.mockingDetails() should be a mock, but is an instance of " + toInspect.getClass() + "!"); 86 } 87 } 88 } 89 90