Vog*_*612 6 java eclipse junit types initialization
我认为这会相对容易,但唉,似乎不是.
我目前正在使用Java EE 6在我的项目中为类似Facade的结构编写单元测试.
对于测试,我使用Junit 4.11,Eclipse Kepler作为IDE.
从我所看到的情况来看,双支撑初始化似乎有些"错误",但我不知道如何能够解释为什么它不起作用,因为我认为应该这样做.
为了达到目的,我使用以下Class在集中的位置进行转换:
package com.example-company.util.converters;
import java.util.HashMap;
import java.util.Map;
import com.example-company.model.Location;
import com.example-company.model.Right;
public final class ModelConverters {
private static final Map<Class<?>, ModelConverter<?, String>> modelConverterBacking = new HashMap<Class<?>, ModelConverter<?, String>>();
static {
modelConverterBacking.put(Right.class, new RightConverter());
modelConverterBacking.put(Location.class, new LocationConverter());
};
public static <T> String convert(final T input)
throws IllegalStateException {
@SuppressWarnings("unchecked")
ModelConverter<T, String> modelConverter = (ModelConverter<T, String>) modelConverterBacking
.get(input.getClass());
if (modelConverter == null) {
throw new IllegalStateException("No mapping found for "
+ input.getClass());
}
return modelConverter.convertToView(input);
}
}
Run Code Online (Sandbox Code Playgroud)
就此而言,它主要是使用泛型和静态地图.现在我决定为此写一些单元测试.下面的类略有缩短,所有不能重现问题的测试用例都被删除了.
package com.example-company.test.unit.util.converters;
import static org.junit.Assert.assertEquals;
import com.example-company.model.Location;
import com.example-company.util.converters.ModelConverters;
import org.junit.Test;
public class ModelConvertersFacadeTests {
@Test
public void test_MappingForLocationExists() {
final Location stub = new Location() {
{
setLocationName("");
}
};
String actual = ModelConverters.convert(stub);
assertEquals("", actual);
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切都很好,没有什么应该真的发生,至少不是我现在得到的.那就是:IllegalStateException以下堆栈跟踪的幻想:
java.lang.IllegalStateException: No mapping found for class com.example-company.test.unit.util.converters.ModelConvertersFacadeTests$1
at com.example-company.util.converters.ModelConverters.convert(ModelConverters.java:23)
at com.example-company.test.unit.util.converters.ModelConvertersFacadeTests.test_MappingForLocationExists(ModelConvertersFacadeTests.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Run Code Online (Sandbox Code Playgroud)
我做的第一件事就是再次运行它,然后设置一个断点来检查内部发生了什么 ModelConverters#convert()
我有点松了一口气:

似乎input.getClass()回归ModelConvertersFacadeTests.但为什么不回归com.example-company.model.Location呢?

Sim*_*erg 13
看来input.getClass()返回ModelConvertersFacadeTests
事实并非如此.您的stacktrace说这是班级:
com.示例,company.test.unit.util.converters.ModelConvertersFacadeTests $ 1
注意$1最后.这意味着您的类是匿名的(它没有自己的名称)内部类.
在this$0我们在截图中看到仅仅是一个参考的外部类.
每次你都new SomeClass() { ... }在创建一个匿名的内部类.
双支撑初始化本身与此无关.每次使用双括号初始化时,您也会创建一个匿名内部类.
你Map有一个映射for Right.class和Location.class,但它没有这两个类的子类的映射.
static {
modelConverterBacking.put(Right.class, new RightConverter());
modelConverterBacking.put(Location.class, new LocationConverter());
};
Run Code Online (Sandbox Code Playgroud)
您可以做的(不是说这是最好的方法)是循环遍历地图的键并检查:
mapKey.isAssignableFrom(input.getClass())
Run Code Online (Sandbox Code Playgroud)
当返回true时,您知道您有一个类mapKey或者您有一个子类.
您也可以循环遍历超类并实现传入的对象的接口,而不是循环遍历映射键,modelConverterBacking.get并对每个接口执行查找.效果是一样的.
您当前的代码是:
final Location stub = new Location() {
{
setLocationName("");
}
};
Run Code Online (Sandbox Code Playgroud)
如果您改为:
final Location stub = new Location();
stub.setLocationName("");
Run Code Online (Sandbox Code Playgroud)
然后你不创建任何匿名内部类,因此不会有这个问题.
但是,即使您只是这样做:
final Location stub = new Location() {};
stub.setLocationName("");
Run Code Online (Sandbox Code Playgroud)
然后你有一个匿名的内部类,这将导致你的问题.
这是非常重要的不要混淆了两个类ModelConvertersFacadeTests$1和ModelConvertersFacadeTests.