/** * Creates a mock of the given type. * * <p>There is no .class literal for generic types. Therefore you need to pass the raw type when * mocking generic types. E.g. Mock<List<String>> myMock = new MockObject("myMock", * List.class, this); * * <p>If the mocked type does not correspond to the declared type, a ClassCastException will occur * when the mock is used. * * <p>If no name is given the un-capitalized type name + Mock is used, e.g. myServiceMock * * @param name The name of the mock, e.g. the field-name, null for the default * @param mockedType The mock type that will be proxied, use the raw type when mocking generic * types, not null * @param testObject The test object, not null */ @SuppressWarnings({"unchecked"}) public MockObject(String name, Class<?> mockedType, Object testObject) { if (isBlank(name)) { this.name = uncapitalize(mockedType.getSimpleName()) + "Mock"; } else { this.name = name; } this.mockedType = (Class<T>) mockedType; this.oneTimeMatchingBehaviorDefiningInvocations = createOneTimeMatchingBehaviorDefiningInvocations(); this.alwaysMatchingBehaviorDefiningInvocations = createAlwaysMatchingBehaviorDefiningInvocations(); this.chainedMocksPerName = new HashMap<String, Mock<?>>(); Scenario scenario = getScenario(testObject); if (scenario.getTestObject() != testObject) { scenario.reset(); getMatchingInvocationBuilder().reset(); scenario.setTestObject(testObject); } this.mockProxy = createMockProxy(); }