Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 无法检测具有Kotlin和@AutoConfigureMockMvc的测试类的默认配置类_Spring Boot_Kotlin - Fatal编程技术网

Spring boot 无法检测具有Kotlin和@AutoConfigureMockMvc的测试类的默认配置类

Spring boot 无法检测具有Kotlin和@AutoConfigureMockMvc的测试类的默认配置类,spring-boot,kotlin,Spring Boot,Kotlin,我对Kotlin进行了一次尝试,并将下面在Java中运行良好的测试转换为Kotlin。使用IntelliJ转换工具转换测试后,我尝试运行它,但出现以下错误: 22:32:19.476 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.test.app.web.Develop

我对Kotlin进行了一次尝试,并将下面在Java中运行良好的测试转换为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一起使用。

当Spring Boot尝试在
developerController测试中内省注释时,堆栈溢出发生

DeveloperControllerTest
kotlin.Metadata
注释<代码>元数据
使用
kotlin.annotation.Retention
进行注释<代码>保留用
kotlin.annotation注释。目标
Target
用自身注释<代码>目标被自身注释是堆栈溢出的原因

使用注释来注释自身是合法的,尽管可能非常罕见。Spring Boot可能更具防御性。

尝试添加

@ComponentScan(basePackages = arrayOf("YourAppBasePackage"))
@SpringBootTest(classes = arrayOf(MyExampleApplication::class))

这为我解决了问题

GitHub的问题,跟踪到: