Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 2: Add HelloWorldService #4

Merged
merged 3 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ Sample used in Adevinta Spain's [Factoria F5](https://factoriaf5.org/) mastercla
* [Setup Continuous Integration using Github Actions](https://github.com/AdevintaSpain/ms-test--factoriaf5-helloworld/pull/1)

### Session 1
* [Apply Adevinta Spain MicroServices Template](https://github.com/AdevintaSpain/ms-test--factoriaf5-helloworld/pull/2)

![Session1](doc/session1.png)

* [Apply Adevinta Spain MicroServices Template](https://github.com/AdevintaSpain/ms-test--factoriaf5-helloworld/pull/2)

### Session 2

![Session2](doc/session2.png)

* [Create HelloWorldService](https://github.com/AdevintaSpain/ms-test--factoriaf5-helloworld/pull/4)

## Run

```
Expand Down
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ dependencies {

// Test
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.mockito:mockito-inline:4.2.0")
testImplementation("org.mockito:mockito-junit-jupiter:4.2.0")
testImplementation("org.assertj:assertj-core:3.22.0")
testImplementation("org.jetbrains.kotlin:kotlin-test:1.6.0")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:1.6.0")
testImplementation("com.nhaarman:mockito-kotlin:1.6.0")

// Integration Test
Expand Down
Binary file added doc/session2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.adevinta.mstestfactoriaf5helloworld.application

class HelloWorldService {

fun hello() = "Hello Coders!!!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.adevinta.mstestfactoriaf5helloworld.infrastructure.configuration

import com.adevinta.mstestfactoriaf5helloworld.application.HelloWorldService
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class ApplicationConfiguration {

@Bean
fun helloWorldService() = HelloWorldService()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.adevinta.mstestfactoriaf5helloworld.infrastructure.controller

import com.adevinta.mstestfactoriaf5helloworld.application.HelloWorldService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/hello")
class HelloWorldController {
class HelloWorldController(
private val helloWorldService: HelloWorldService
) {

@GetMapping
@Suppress("FunctionOnlyReturningConstant")
fun sayHello(): String = "Hello Coders!!!"
fun sayHello(): String {
return helloWorldService.hello()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.adevinta.mstestfactoriaf5helloworld.application

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class HelloWorldServiceTest {

@Test
fun `should say hello`() {
assertThat(HelloWorldService().hello())
.isEqualTo("Hello Coders!!!")
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.adevinta.mstestfactoriaf5helloworld.infrastructure.controller

import com.adevinta.mstestfactoriaf5helloworld.application.HelloWorldService
import com.adevinta.mstestfactoriaf5helloworld.infrastructure.Application
import com.nhaarman.mockito_kotlin.doReturn
import com.nhaarman.mockito_kotlin.whenever
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
Expand All @@ -15,13 +19,19 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
@ContextConfiguration(classes = [Application::class])
class HelloWorldControllerTest {

@MockBean
private lateinit var helloWorldService: HelloWorldService

@Autowired
private lateinit var mvc: MockMvc

@Test
fun `should say hello`() {
val helloWorldMessage = "Hello Coders from Mock HelloWorldService!!!"
doReturn(helloWorldMessage).whenever(helloWorldService).hello()

mvc.perform(get("/hello"))
.andExpect(status().isOk)
.andExpect(content().string(equalTo("Hello Coders!!!")))
.andExpect(content().string(equalTo(helloWorldMessage)))
}
}