Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3229: Resolve test generic arguments #3257

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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());
}
}