2014-09-01 62 views
6

Bunun göreceli olarak kolay olacağını düşündüm, ama ne yazık ki görünmüyor.Double Brace initialization Tür Confusion

Şu anda IDE olarak Eclipse Kepler ile JUnit 4.11 kullanmak Testler için Java EE 6.
kullanarak Projesi bir Cephe benzeri yapı için Birim-Testleri yazıyorum. Görebildiğim kadarıyla

, çift ayracı başlatma ile "yanlış" bir şey var gibi görünüyor, ama ben sanırım gerekir gibi çalışmıyor neden benim parmak koymak için yeterli bilgili değilim.

Ben merkezi yerde dönüşümleri yapmak için aşağıdaki Class kullanıyorum, noktaya gelmek için:

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); 
    } 
} 

Bildiğim kadarıyla bu çoğunlukla jenerik ve statik harita ile oynuyor gider. Şimdi bunun için birkaç ünite testi yazmam gerektiğine karar verdim. Aşağıdaki sınıf biraz kısaltılmış, sorunu yeniden üretmeyen tüm test senaryoları kaldırılmıştır.

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); 
    } 
} 

Şimdiye kadar çok iyi, hiçbir şey gerçekten olmamalı, en azından şu anda sahip olduğum şey değil. Bunlar da: Aşağıdaki stacktrace sahip bir fantezi IllegalStateException: yaptım

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) 

ilk şey, yeniden çalıştırın, sonra hafifçe flabberghasted var ne ModelConverters#convert()

içeride neler olduğunu incelemek için bir kesme noktası batıyordu ben:

Debug Perspective: Expressions

o görünüyor input.getClass() yeniden ModelConvertersFacadeTests döner. Ama neden com.example-company.model.Location dönmüyor?

Full Debug Perspective, names censored

+0

[Genellikle çift kaşlı ayraçlamayı kullanmaya dikkat etmelisiniz] (http://stackoverflow.com/q/924285/521799) –

cevap