无法使用Kotlin和@AutoConfigureMockMvc检测测试类的默认配置类
我正在给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将启动并尝试运行,但它会失败,因为它不能autowire MockMvc
。
我使用Kotlin 1.0.1-2和Spring Boot 1.4.0-M2。
Spring Boot尝试在DeveloperControllerTest
上检查注释时发生堆栈溢出。
DeveloperControllerTest
使用kotlin.Metadata
注释。 Metadata
注释为kotlin.annotation.Retention
。 Retention
用kotlin.annotation.Target
注释。 Target
和Target
是用自己注释的。 被注释的Target
是堆栈溢出的原因。
这是合法的,虽然可能很不寻常,用于注释本身的注释。 春季靴子应该更有防御性。
尝试添加
@ComponentScan(basePackages = arrayOf("YourAppBasePackage")) @SpringBootTest(classes = arrayOf(MyExampleApplication::class))
这为我解决了这个问题
- 使用Kotlin的Spring Boot 2.0.0.M2应用程序的UnsatisfiedDependencyException
- Spring Boot Controller语言环境不随参数更改
- Spring Boot:Boot Run不能添加addResources = true
- Spring Boot无法在IntelliJ中运行单个测试
- ReactiveCrudRespository永远不会从SaveAll返回
- Hibernate @OneToMany连接表抛出StackOverflowException
- Kotlin编译器抱怨在属性定义中使用SPeL表达式。 为什么?
- WebFlux功能:如何检测一个空的通量并返回404?
- Kotlin和DynamoDBMapper SaveBehavior