Reputation: 13395
I am new to kotlin
(basically started today)
I want to write a dsl
like builder for my GRLMessage
class.
data class GRLMessage(var method: GRLMethod, var headers: Map<String, String>, var multipart: Object) {
fun message(init: GRLMessage.() -> Unit) : GRLMessage {
init()
return this
}
fun method(init: GRLMessage.() -> GRLMethod) : GRLMessage {
method = init()
return this
}
fun headers(init: GRLMessage.() -> Unit) : GRLMessage {
init()
return this
}
fun header(init: GRLMessage.() -> Pair<String, String>) : GRLMessage {
headers.plus(init())
return this
}
fun multipart(init: GRLMessage.() -> Object) : GRLMessage {
multipart = this.init()
return this
}
}
In order to check it I added test.
import org.junit.Assert.*
import org.junit.*
class GRLMessageTest {
data class DummyMultipart(val field: String) {}
@Test fun grlMessageBuilderTest() {
val grlMessage = GrlMessage().message {
method { GRLMethod.POST }
headers {
header { Pair("contentType", "object") }
header { Pair("objectType", "DummyMultipart") }
}
multipart { DummyMultipart("dummy") }
}
val multipart = DummyMultipart("dummy")
val headers = mapOf(
Pair("contentType", "object"),
Pair("objectType", "DummyMultipart")
)
val method = GRLMethod.POST
assertEquals(multipart, grlMessage.multipart)
assertEquals(headers, grlMessage.headers)
assertEquals(method, grlMessage.method)
}
}
Both classes are in the same packages (relatively - one is in src/main/kotlin
, another one is in src/test/kotlin
).
When I try to build application using gradle build
it fails on compileTestKotlin
task with errors
GRLMessageTest.kt: (13, 26): Unresolved reference: GrlMessage
GRLMessageTest.kt: (14, 13): Unresolved reference: method
GRLMessageTest.kt: (15, 13): Unresolved reference: headers
GRLMessageTest.kt: (17, 17): Unresolved reference: header
GRLMessageTest.kt: (19, 13): Unresolved reference: multipart
What is the problem?
Here is the layout
Here is the build.gradle
for that concrete subproject and plugin
Upvotes: 1
Views: 8950
Reputation: 85946
You have a naming mismatch between your class and the reference to the class from your tests.
Your class is named GRLMessage
but in your test you reference it as GrlMessage
and Kotlin is case sensitive. These are therefore not considered the same. You need to use exactly the same identifier in both.
Upvotes: 5