测试 Lombok 生成的数据结构
Test Lombok-generated data structure
我有以下 class 描述一个元组,为此使用 lombok
:
@Data
public class Pair<K, V> {
private final K key;
private V value;
}
然后我想为这个class写一个测试,使用JUnit
如下:
public class PairTest {
@Test
void pairCanBeCreatedAndHoldIntegerValues() {
Pair<Integer,Integer> myTestPair = new Pair<Integer, Integer>(0);
myTestPair.setValue(5);
assertEquals(5, myTestPair.getValue());
assertEquals(0, myTestPair.getKey());
}
}
然而,尽管 lombok 自动生成的 get 方法应该 return 一个 int
,我可以看到我的 IDE 抱怨:
Error:(12, 9) java: reference to assertEquals is ambiguous
both method assertEquals(java.lang.Object,java.lang.Object) in org.junit.jupiter.api.Assertions and method assertEquals(int,int) in org.junit.jupiter.api.Assertions match
尽管我 知道 assertEquals
需要两个 int
,我正在提供。
如何在 Java
中执行此操作?
该行为的原因称为 autoboxing\unboxing。
Autoboxing is the automatic conversion that the Java compiler makes
between the primitive types and their corresponding object wrapper
classes. For example, converting an int to an Integer, a double to a
Double, and so on. If the conversion goes the other way, this is
called unboxing.
myTestPair
对象 returns Integer
是 int
的包装器,因此编译器不知道要使用什么重载方法:assertEquals(Object, Object)
或 assertEquals(int, int)
。你需要让编译器知道它,使用:
assertEquals(5, myTestPair.getValue().intValue());
assertEquals(0, myTestPair.getKey().intValue());
或
assertEquals((Integer)5, myTestPair.getValue());
assertEquals((Integer)0, myTestPair.getKey());
或
assertEquals(Integer.valueOf(5), myTestPair.getValue());
assertEquals(Integer.valueOf(0), myTestPair.getKey());
我有以下 class 描述一个元组,为此使用 lombok
:
@Data
public class Pair<K, V> {
private final K key;
private V value;
}
然后我想为这个class写一个测试,使用JUnit
如下:
public class PairTest {
@Test
void pairCanBeCreatedAndHoldIntegerValues() {
Pair<Integer,Integer> myTestPair = new Pair<Integer, Integer>(0);
myTestPair.setValue(5);
assertEquals(5, myTestPair.getValue());
assertEquals(0, myTestPair.getKey());
}
}
然而,尽管 lombok 自动生成的 get 方法应该 return 一个 int
,我可以看到我的 IDE 抱怨:
Error:(12, 9) java: reference to assertEquals is ambiguous
both method assertEquals(java.lang.Object,java.lang.Object) in org.junit.jupiter.api.Assertions and method assertEquals(int,int) in org.junit.jupiter.api.Assertions match
尽管我 知道 assertEquals
需要两个 int
,我正在提供。
如何在 Java
中执行此操作?
该行为的原因称为 autoboxing\unboxing。
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
myTestPair
对象 returns Integer
是 int
的包装器,因此编译器不知道要使用什么重载方法:assertEquals(Object, Object)
或 assertEquals(int, int)
。你需要让编译器知道它,使用:
assertEquals(5, myTestPair.getValue().intValue());
assertEquals(0, myTestPair.getKey().intValue());
或
assertEquals((Integer)5, myTestPair.getValue());
assertEquals((Integer)0, myTestPair.getKey());
或
assertEquals(Integer.valueOf(5), myTestPair.getValue());
assertEquals(Integer.valueOf(0), myTestPair.getKey());