Skip to content

Commit

Permalink
Kotlin and Groovy versions of Micronaut Dependency Injection types (#…
Browse files Browse the repository at this point in the history
…1227)

Close #1082
  • Loading branch information
sdelamo authored Jan 16, 2023
1 parent da6a412 commit 9e07ad5
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package example.micronaut

import io.micronaut.core.annotation.NonNull
import jakarta.inject.Singleton

@Singleton
class MessageService {

@NonNull
String compose() {
"Hello World"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package example.micronaut.constructor

import example.micronaut.MessageService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces

@Controller("/constructor")
class MessageController {
private final MessageService messageService

MessageController(MessageService messageService) {
this.messageService = messageService
}

@Get
@Produces(MediaType.TEXT_PLAIN)
String index() {
messageService.compose()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package example.micronaut.field

import example.micronaut.MessageService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import jakarta.inject.Inject

@Controller("/field")
class MessageController {
@Inject
MessageService messageService

@Get
@Produces(MediaType.TEXT_PLAIN)
String index() {
messageService.compose()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package example.micronaut.methodparameter

import example.micronaut.MessageService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import jakarta.inject.Inject

@Controller("/setter")
class MessageController {
private MessageService messageService

@Get
@Produces(MediaType.TEXT_PLAIN)
String index() {
return messageService.compose()
}

@Inject
void populateMessageService(MessageService messageService) {
this.messageService = messageService
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.MediaType
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

import static org.junit.jupiter.api.Assertions.assertEquals

@MicronautTest
class MessageControllerTest {
@Inject
@Client("/")
HttpClient httpClient

@ParameterizedTest
@ValueSource(strings = ["/constructor", "/field", "/setter"])
void validateMessageControllers(String path) {
assertEquals("Hello World", httpClient.toBlocking().retrieve(
HttpRequest.GET(path).accept(MediaType.TEXT_PLAIN))
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package example.micronaut

import io.micronaut.core.annotation.NonNull
import jakarta.inject.Singleton

@Singleton
class MessageService {
@NonNull
fun compose() = "Hello World"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example.micronaut.constructor

import example.micronaut.MessageService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces

@Controller("/constructor")
class MessageController(val messageService: MessageService) {
@Get
@Produces(MediaType.TEXT_PLAIN)
fun index() = messageService.compose()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package example.micronaut.field

import example.micronaut.MessageService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import jakarta.inject.Inject

@Controller("/field")
class MessageController {
@Inject
lateinit var messageService: MessageService

@Get
@Produces(MediaType.TEXT_PLAIN)
fun index() = messageService.compose()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package example.micronaut.methodparameter

import example.micronaut.MessageService
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
import jakarta.inject.Inject

@Controller("/setter")
class MessageController {
var messageService: MessageService? = null

@Get
@Produces(MediaType.TEXT_PLAIN)
fun index() = messageService?.compose()

@Inject
fun populateMessageService(messageService: MessageService) {
this.messageService = messageService
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.MediaType
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource

@MicronautTest
class MessageControllerTest {
@Inject
@field:Client("/")
lateinit var httpClient: HttpClient

@ParameterizedTest
@ValueSource(strings = ["/constructor", "/field", "/setter"])
fun validateMessageControllers(path: String) {
val request = HttpRequest.GET<String>(path).accept(MediaType.TEXT_PLAIN)
assertEquals("Hello World", httpClient.toBlocking().retrieve(request))
}
}
2 changes: 1 addition & 1 deletion guides/micronaut-dependency-injection-types/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"tags": ["junit","di"],
"categories": ["Getting Started"],
"publicationDate": "2022-09-09",
"languages": ["java"],
"testFramework": "junit",
"apps": [
{
"name": "default",
Expand Down

0 comments on commit 9e07ad5

Please sign in to comment.