无法使用Kotlin和@AutoConfigureMockMvc检测测试类的默认配置类

时间:2016-05-09 14:54:00

标签: spring-boot kotlin

我正在给Kotlin一个旋转,并且正在将下面的测试转换为Kotlin。在使用IntelliJ转换工具转换测试后,我尝试运行它,但是我收到了这个错误:

22:32:19.476 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.test.app.web.DeveloperControllerTest]
22:32:19.486 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
22:32:19.494 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
22:32:19.517 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.test.app.web.DeveloperControllerTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
22:32:19.539 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.test.app.web.DeveloperControllerTest], using SpringBootContextLoader
22:32:19.543 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.test.app.web.DeveloperControllerTest]: class path resource [com/test/app/web/DeveloperControllerTest-context.xml] does not exist
22:32:19.544 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.test.app.web.DeveloperControllerTest]: class path resource [com/test/app/web/DeveloperControllerTestContext.groovy] does not exist
22:32:19.544 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.test.app.web.DeveloperControllerTest]: no resource found for suffixes {-context.xml, Context.groovy}.
22:32:19.545 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.test.app.web.DeveloperControllerTest]: DeveloperControllerTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
java.lang.StackOverflowError
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964)
at java.lang.reflect.WeakCache.containsValue(WeakCache.java:175)
at java.lang.reflect.Proxy.isProxyClass(Proxy.java:791)
at java.lang.reflect.Proxy.getInvocationHandler(Proxy.java:815)
at sun.reflect.annotation.AnnotationInvocationHandler.asOneOfUs(AnnotationInvocationHandler.java:226)
at sun.reflect.annotation.AnnotationInvocationHandler.equalsImpl(AnnotationInvocationHandler.java:201)
at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:64)
at com.sun.proxy.$Proxy13.equals(Unknown Source)
at java.util.HashMap.putVal(HashMap.java:634)
at java.util.HashMap.put(HashMap.java:611)
at java.util.HashSet.add(HashSet.java:219)
at org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.collectElementAnnotations(ImportsContextCustomizer.java:239)
at org.springframework.boot.test.context.ImportsContextCustomizer$ContextCustomizerKey.collectClassAnnotations(ImportsContextCustomizer.java:226)

Java测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class DeveloperControllerTest {
    @Autowired
    private MockMvc mvc;

    @Test
    public void createNewDeveloper() throws Exception {
        mvc.perform(
                post("/api/v1/developers")
                        .param("username", "boaty")
                        .param("email", "boaty@mcboatface.org")
                        .param("password", "123loveboats")
                        .param("passwordConfirmation", "123loveboats")
        ).andExpect(status().isCreated());
    }
}

转换为Kotlin:

@RunWith(SpringRunner::class)
@SpringBootTest
@AutoConfigureMockMvc
class DeveloperControllerTest {
    @Autowired
    lateinit var mvc: MockMvc

    @Test
    @Throws(Exception::class)
    fun createNewDeveloper() {
        mvc.perform(
                post("/api/v1/developers")
                        .param("username", "boaty")
                        .param("email", "boaty@mcboatface.org")
                        .param("password", "123loveboats")
                        .param("passwordConfirmation", "123loveboats"))
                .andExpect(status().isCreated)
    }
}

我注意到这一点是,如果我删除注释AutoConfigureMockMvc Spring将启动并尝试运行,但它将失败,因为它无法自动装配MockMvc

我正在使用Kotlin 1.0.1-2和Spring Boot 1.4.0-M2。

2 个答案:

答案 0 :(得分:1)

当Spring Boot尝试对DeveloperControllerTest上的注释进行内省时,会发生堆栈溢出。

DeveloperControllerTest使用kotlin.Metadata进行注释。 Metadata注释了kotlin.annotation.RetentionRetention注释了kotlin.annotation.TargetTarget注释了自己。被自身注释的Target是堆栈溢出的原因。

注释用于注释自身是合法的,尽管可能很不寻常。 Spring Boot应该更具防御性。

答案 1 :(得分:1)

尝试添加

Calendar min = (Calendar) minDate.clone();
Calendar max = (Calendar) maxDate.clone();
Calendar selected = (Calendar) selectedDate.clone();
min.set(Calendar.HOUR_OF_DAY, 0);
max.set(Calendar.HOUR_OF_DAY, 23);
selected.set(Calendar.HOUR_OF_DAY, 12);

DatePickerDialog datePickerDialog =
      new DatePickerDialog(context, callBack, selected);

if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
  datePickerDialog.getDatePicker().setCalendarViewShown(false);
}

datePickerDialog.getDatePicker().setMinDate(min.getTimeInMillis());
datePickerDialog.getDatePicker().setMaxDate(max.getTimeInMillis());
datePickerDialog.show();

这解决了我的问题