Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ repository on GitHub.
a custom comment character using the new `commentCharacter` attribute.
* Improve error message when using `@ParameterizedClass` with field injection and not
providing enough arguments.
* Allow calling `TypedArgumentConverter` constructor for `@Nullable T` target types
without having to cast class literals to `Class<@Nullable T>`.


[[release-notes-6.0.1-junit-vintage]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.apiguardian.api.API.Status.STABLE;

import org.apiguardian.api.API;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.support.FieldContext;
Expand Down Expand Up @@ -43,7 +44,8 @@ public abstract class TypedArgumentConverter<S, T extends @Nullable Object> impl
* @param targetType the type of the target object to create from the source;
* never {@code null}
*/
protected TypedArgumentConverter(Class<S> sourceType, Class<T> targetType) {
protected TypedArgumentConverter(Class<S> sourceType,
@SuppressWarnings("NullableProblems") Class<@NonNull T> targetType) {
this.sourceType = Preconditions.notNull(sourceType, "sourceType must not be null");
this.targetType = Preconditions.notNull(targetType, "targetType must not be null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.stream.Stream;

import org.assertj.core.api.Condition;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -972,7 +973,8 @@ void test2() {
}
}

private static class CustomIntegerToStringConverter extends TypedArgumentConverter<Integer, String> {
@NullMarked
private static class CustomIntegerToStringConverter extends TypedArgumentConverter<Integer, @Nullable String> {

CustomIntegerToStringConverter() {
super(Integer.class, String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -2629,6 +2630,7 @@ void testWithIso639(@ConvertWith(Iso639Converter.class) Locale locale) {
assertEquals("", locale.getCountry());
}

@NullMarked
static class Iso639Converter extends TypedArgumentConverter<String, Locale> {

Iso639Converter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -203,6 +204,7 @@ void stringToPrimitiveLong(@StringLength long length) {
private @interface StringLength {
}

@NullMarked
private static class StringLengthArgumentConverter extends TypedArgumentConverter<String, Integer> {

StringLengthArgumentConverter() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2015-2025 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.jupiter.params.converter

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertNull
import org.junit.jupiter.api.extension.ParameterContext
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`

class TypedArgumentConverterKotlinTests {
@Test
fun converts() {
val parameterContext = mock(ParameterContext::class.java)
val parameter = this.javaClass.getDeclaredMethod("foo", String::class.java).parameters[0]
`when`(parameterContext.parameter).thenReturn(parameter)

assertNull(NullableTypeConverter().convert(null, parameterContext))
assertEquals("null", NonNullableTypeConverter().convert(null, parameterContext))
}

@Suppress("unused")
private fun foo(param: String) = Unit

class NullableTypeConverter : TypedArgumentConverter<String, String?>(String::class.java, String::class.java) {
override fun convert(source: String?) = source
}

class NonNullableTypeConverter : TypedArgumentConverter<String, String>(String::class.java, String::class.java) {
override fun convert(source: String?) = source.toString()
}
}