@@ -46,9 +46,8 @@ suspend fun testContextManager(): TestContextManager =
46
46
47
47
val SpringExtension = SpringTestExtension (SpringTestLifecycleMode .Test )
48
48
49
- class SpringTestExtension (private val mode : SpringTestLifecycleMode ) : TestCaseExtension, SpecExtension {
50
-
51
- constructor () : this (SpringTestLifecycleMode .Test )
49
+ class SpringTestExtension (private val mode : SpringTestLifecycleMode = SpringTestLifecycleMode .Test ) : TestCaseExtension,
50
+ SpecExtension {
52
51
53
52
var ignoreSpringListenerOnFinalClassWarning: Boolean = false
54
53
@@ -89,25 +88,23 @@ class SpringTestExtension(private val mode: SpringTestLifecycleMode) : TestCaseE
89
88
* Check https://github.com/kotest/kotest/issues/950#issuecomment-524127221
90
89
* for an in-depth explanation. Too much to write here
91
90
*/
92
- private fun method (testCase : TestCase ): Method {
93
- return if (Modifier .isFinal(testCase.spec::class .java.modifiers)) {
94
- if (! ignoreFinalWarning) {
95
- println (" Using SpringListener on a final class. If any Spring annotation fails to work, try making this class open." )
96
- }
97
- // the method here must exist since we can't add our own
98
- this @SpringTestExtension::class .java.methods.firstOrNull { it.name == " intercept" }
99
- ? : error(" Could not find method 'intercept' to attach spring lifecycle methods to" )
100
- } else {
101
- val methodName = methodName(testCase)
102
- val fakeSpec = ByteBuddy ()
103
- .subclass(testCase.spec::class .java)
104
- .defineMethod(methodName, String ::class .java, Visibility .PUBLIC )
105
- .intercept(FixedValue .value(" Foo" ))
106
- .make()
107
- .load(this ::class .java.classLoader, ClassLoadingStrategy .Default .CHILD_FIRST )
108
- .loaded
109
- fakeSpec.getMethod(methodName)
91
+ private fun method (testCase : TestCase ): Method = if (Modifier .isFinal(testCase.spec::class .java.modifiers)) {
92
+ if (! ignoreFinalWarning) {
93
+ println (" Using SpringListener on a final class. If any Spring annotation fails to work, try making this class open." )
110
94
}
95
+ // the method here must exist since we can't add our own
96
+ this @SpringTestExtension::class .java.methods.firstOrNull { it.name == " intercept" }
97
+ ? : error(" Could not find method 'intercept' to attach spring lifecycle methods to" )
98
+ } else {
99
+ val methodName = methodName(testCase)
100
+ val fakeSpec = ByteBuddy ()
101
+ .subclass(testCase.spec::class .java)
102
+ .defineMethod(methodName, String ::class .java, Visibility .PUBLIC )
103
+ .intercept(FixedValue .value(" Foo" ))
104
+ .make()
105
+ .load(this ::class .java.classLoader, ClassLoadingStrategy .Default .CHILD_FIRST )
106
+ .loaded
107
+ fakeSpec.getMethod(methodName)
111
108
}
112
109
113
110
/* *
@@ -116,7 +113,6 @@ class SpringTestExtension(private val mode: SpringTestLifecycleMode) : TestCaseE
116
113
*/
117
114
internal fun safeClassName (kclass : KClass <* >) {
118
115
// these are names java won't let us use but are ok from kotlin
119
- val illegals = listOf (" import" , " finally" , " catch" , " const" , " final" , " inner" , " protected" , " private" , " public" )
120
116
if (kclass.java.name.split(' .' ).any { illegals.contains(it) })
121
117
error(" Spec package name cannot contain a java keyword: ${illegals.joinToString(" ," )} " )
122
118
}
@@ -125,11 +121,18 @@ class SpringTestExtension(private val mode: SpringTestLifecycleMode) : TestCaseE
125
121
* Generates a fake method name for the given [TestCase].
126
122
* The method name is taken from the test case path.
127
123
*/
128
- internal fun methodName (testCase : TestCase ): String {
129
- return testCase.descriptor.path(false ).value.replace(" [^a-zA-Z_0-9]" .toRegex(), " _" ).let {
124
+ internal fun methodName (testCase : TestCase ): String = testCase.descriptor
125
+ .path(false )
126
+ .value
127
+ .replace(methodNameRegex, " _" )
128
+ .let {
130
129
if (it.first().isLetter()) it else " _$it "
131
130
}
132
- }
131
+
132
+ private val illegals =
133
+ listOf (" import" , " finally" , " catch" , " const" , " final" , " inner" , " protected" , " private" , " public" )
134
+
135
+ private val methodNameRegex = " [^a-zA-Z_0-9]" .toRegex()
133
136
134
137
private val ignoreFinalWarning =
135
138
ignoreSpringListenerOnFinalClassWarning ||
0 commit comments