This person's article Make a note of the result of the trial. The language is Scala, but it might be useful when you want to use Spring Boot.
build.gradle
// Scala
apply plugin: 'scala'
dependencies {
compile 'org.scala-lang:scala-library:2.12.6'
}
ApplicationScala.scala
package hello
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class ApplicationScala {}
object ApplicationScala {
def main(args: Array[String]): Unit = SpringApplication.run(classOf[ApplicationScala], args: _*)
}
I'm still not sure if the test code will cover the main function that launches Spring Boot.
ScalaController.scala
package hello
import org.springframework.web.bind.annotation.{RequestMapping, RequestMethod, RestController}
@RestController
@RequestMapping(Array("/scala"))
class ScalaController {
@RequestMapping(method = Array(RequestMethod.GET))
def sample = "sample from scala."
}
Using Spring Boot annotations, check the validity of the response as the result of API execution through URL mapping, not just the return value of the method.
ScalaControllerTest.scala
package hello
import org.hamcrest.Matchers.equalTo
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
@RunWith(classOf[SpringRunner])
@SpringBootTest
@AutoConfigureMockMvc
class ScalaControllerTest {
@Autowired
val mvc: MockMvc = null
@Test
@throws[Exception]
def sampleGet_Ok(): Unit =
mvc.perform(MockMvcRequestBuilders.get("/scala").accept(MediaType.APPLICATION_JSON))
.andExpect(status.isOk)
.andExpect(content.string(equalTo("sample from scala.")))
}
Recommended Posts