Artikel dieser Person Notieren Sie sich das Ergebnis der Studie. Die Sprache ist Scala, aber es kann nützlich sein, wenn Sie Spring Boot verwenden möchten.
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: _*)
}
Ich bin mir immer noch nicht sicher, ob der Testcode die Hauptfunktion abdeckt, mit der Spring Boot gestartet wird.
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."
}
Überprüfen Sie mithilfe von Spring Boot-Annotationen die Gültigkeit der Antwort nicht nur als Rückgabewert der Methode, sondern auch als Ergebnis der Ausführung der API über die URL-Zuordnung.
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