Git Product home page Git Product logo

kd4smt's Introduction

kd4smt - Kotlin dsl for spring mvc test

Imagine that you have spring mvc controller

@Controller
class HelloController {

    @GetMapping("/hello")
    fun hello(@RequestParam name: String, modelMap: ModelMap): String {
        modelMap["name"] = name
        return "hello"
    }

    @PostMapping("/hello")
    fun helloPost(dto: HelloPostDto, modelMap: ModelMap): String = "hello"

}

data class HelloPostDto(var surname:String = "World")

Standart spring mvc test (see StandardControllerTest):

 val mvc: MockMvc
 
 
    @Test
     fun hello() {
         val requestBuilder = get("/hello?name={1}", "Petr")
         val className = MockMvcResultMatchers.xpath("""//span[@class="name"]""")
         val actions: ResultActions = mvc.perform(requestBuilder)
                 .andExpect(MockMvcResultMatchers.status().isOk)
                 .andExpect(MockMvcResultMatchers.xpath("//h1").nodeCount(1))
                 .andExpect(className.nodeCount(1))
                 .andExpect(className.string("Petr"))
                 .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
         val result: MvcResult = actions.andReturn()
 
         val modelAndView: ModelAndView = result.modelAndView
         assertEquals("hello", modelAndView.viewName)
         assertEquals(1, modelAndView.model.size)
         assertEquals("Petr", modelAndView.model["name"])
     }

With this library dsl spring mvc test (see DslControllerTest):

 val mvc: MockMvc
 
 @Test
 fun helloGet() {
         mockMvc.performGet("/hello?name=Petr") {
             expectStatus { isOk }
             expectContent { contentTypeCompatibleWith(MediaType.TEXT_HTML) }
             expectViewName("hello")
 
             expectModel {
                 size<Any>(1)
                 attribute("name", "Petr")
             }
 
             expectXPath("//h1") {
                 nodeCount(1)
             }
 
             expectXPath("""//span[@class="name"]""") {
                 nodeCount(1)
                 string("Petr")
             }
         }
     }

or if you implement MockMvcProvider (see DslControllerTest):

 override val mvc: MockMvc
 
 @Test
 fun helloGetExpression() = performGet("/hello?name=Petr") {
         expectStatus { isOk }
         expectContent { contentTypeCompatibleWith(MediaType.TEXT_HTML) }
         expectViewName("hello")
 
         expectModel {
             size<Any>(1)
             attribute("name", "Petr")
         }
 
         expectXPath("//h1") {
             nodeCount(1)
         }
 
         expectXPath("""//span[@class="name"]""") {
             nodeCount(1)
             string("Petr")
         }
     }
     
    @Test
    fun helloPost() = performPost("/hello", requestInit = {
        contentType(MediaType.APPLICATION_FORM_URLENCODED)
        param("surname", "Balat")
    }) {
        expectStatus { isOk }
        expectContent { contentTypeCompatibleWith(MediaType.TEXT_HTML) }
        expectViewName("hello")

        expectModel {
            size<Any>(2)
            attribute("helloPostDto", HelloPostDto("Balat"))
        }

        expectModel<HelloPostDto>("helloPostDto") {
            assertEquals("Balat", surname)
        }

        expectXPath("//h1") {
            nodeCount(1)
        }

        expectXPath("""//span[@class="name"]""") {
            nodeCount(1)
            string("Balat")
        }
    }

##How to use

Gradle:

repositories {
    url("https://mymavenrepo.com/repo/plFabfZlZhQO7UjsJKDc/")
}
    
...

testCompile "cz.petrbalat:kd4smt:0.4.1"

Maven:

<dependency>
    <groupId>cz.petrbalat</groupId>
    <artifactId>kd4smt</artifactId>
    <version>0.4.1</version>
    <scope>test</scope>
</dependency>


...


<repository>
    <id>kd4smt-mvn-repo</id>
    <url>https://mymavenrepo.com/repo/plFabfZlZhQO7UjsJKDc/</url>
    <snapshots>
        <enabled>false</enabled>
        <updatePolicy>always</updatePolicy>
    </snapshots>
</repository>

kd4smt's People

Contributors

petrbalat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.