Skip to content

Commit

Permalink
add support for classes with one constructor but without annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg-pfruender committed Jan 6, 2022
1 parent 2ab3662 commit 93aa0b9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main/java/org/hypoport/mockito/MockInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,36 @@ public static <T> T injectMocks(Class<T> clazz) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
if (shouldBeInjected(constructor.getDeclaredAnnotations()) || constructor.getParameterTypes().length == 0) {
constructor.setAccessible(true);
Class<?>[] parameterTypes = constructor.getParameterTypes();
Object[] mocks = createMocksForParameterTypes(parameterTypes);
T instantiated = (T) constructor.newInstance(mocks);
injectFieldsAndSetters(instantiated, clazz);
return instantiated;
return createObjectAndInjectMocks(clazz, constructor);
}
}
// no annotated constructor found:
if (constructors.length == 1) {
return createObjectAndInjectMocks(clazz, constructors[0]);
}
// we hopefully never get here:
throw new RuntimeException("no constructor found for class " + clazz);
throw new RuntimeException("no usable constructor found for class " + clazz);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static <T> T createObjectAndInjectMocks(Class<T> clazz, Constructor<?> constructor) throws InstantiationException, IllegalAccessException, InvocationTargetException {
T instantiated = createObjectUsingConstructor(constructor);
injectFieldsAndSetters(instantiated, clazz);
return instantiated;
}

private static <T> T createObjectUsingConstructor(Constructor<?> constructor) throws InstantiationException, IllegalAccessException, InvocationTargetException {
constructor.setAccessible(true);
Class<?>[] parameterTypes = constructor.getParameterTypes();
Object[] mocks = createMocksForParameterTypes(parameterTypes);
T instantiated = (T) constructor.newInstance(mocks);
return instantiated;
}

public static void injectFieldsAndSetters(Object object, Class<?> objectClass) throws IllegalAccessException, InvocationTargetException {
injectFields(object, objectClass);
injectSetter(object, objectClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2022 HYPOPORT AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hypoport.mockito.injection;

import javax.inject.Inject;

public class ConstructorInjectionClassWithoutAnnotation {

MyClass toBeInjected1;
Object toBeInjected2;

private ConstructorInjectionClassWithoutAnnotation(MyClass toBeInjected1, Object toBeInjected2) {
this.toBeInjected1 = toBeInjected1;
this.toBeInjected2 = toBeInjected2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ public void injectMocks_with_Class_can_handle_Constructor_Injection() {
Assertions.assertThat(MockUtil.isMock(object.toBeInjected2)).isTrue();
}

@Test
public void injectMocks_with_Class_can_handle_Constructor_Injection_without_annotation() {
ConstructorInjectionClassWithoutAnnotation object = MockInjector.injectMocks(ConstructorInjectionClassWithoutAnnotation.class);

Assertions.assertThat(MockUtil.isMock(object.toBeInjected1)).isTrue();
Assertions.assertThat(MockUtil.isMock(object.toBeInjected2)).isTrue();
}

}

0 comments on commit 93aa0b9

Please sign in to comment.