Skip to content

Commit

Permalink
Fixes #3229: Resolve test generic arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
youssef3wi committed Feb 2, 2024
1 parent 9972676 commit 915f9c8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Class<?> getGenericType(Field field) {
* @param parameter the parameter to inspect
*/
public Class<?> getGenericType(Parameter parameter) {
return getaClass(parameter.getType());
return getaClass(parameter.getParameterizedType());
}

private Class<?> getaClass(Type generic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.junit.Assert.assertEquals;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.Type;
import java.util.*;

Expand Down Expand Up @@ -64,7 +66,24 @@ public void should_deal_with_nested_generics() throws Exception {
assertEquals(Set.class, m.getGenericType(field("multiNested")));
}

@Test
public void should_detect_generics() throws NoSuchMethodException {
// Given
Method method = ClassWithParameter.class.getMethod("process", List.class);
Parameter parameter = method.getParameters()[0];

// When
Class<?> genericType = m.getGenericType(parameter);

// Then
assertEquals(Integer.class, genericType);
}

private Field field(String fieldName) throws SecurityException, NoSuchFieldException {
return this.getClass().getDeclaredField(fieldName);
}

private static class ClassWithParameter {
public void process(List<Integer> integers) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.annotation;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doNothing;

@ExtendWith(MockitoExtension.class)
public class CaptorAnnotationWithPrimitiveTest {
@Mock private Foo foo;

static class Foo {
void doSomething(int value) {}
}

@Test
public void shouldCaptorPrimitive(@Captor ArgumentCaptor<Integer> captor) {
// Given
int value = 1;

// When
doNothing().when(foo).doSomething(captor.capture());

// Then
foo.doSomething(value);
assertEquals(1, captor.getValue());
}
}

0 comments on commit 915f9c8

Please sign in to comment.