Coder Social home page Coder Social logo

mockmvc-and-restdoc's Introduction

SpringBoot + MockMVC + RestDocs + Groovy

Environment

  • JDK 1.8
  • SpringBoot 2.1.7 (Spring 5.1.9)
  • SpringBootRestDocs 2.0.4
  • Gradle 4.10
  • Groovy 2.3.11

1. Use this custom test module

:avaj-core-mock, It provides RestDocs and MockMVC.

  1. You can check and test sample here:

  2. It will be parsed to HTML file for documents when it builds.

    • avaj-core-mock/src/main/asciidoc/mock-and-rest-docs-test.adoc
  3. Run

    • /run.sh to gradle bootRun to run WebApplication
  4. You can open documents !


2. Let's set it up in another module

It can be applied to other modules using :avaj-core-mock.

You can check how to setup other modules in :avaj-engine-service

  1. Setup MockMVC and RestDocs

    • avaj-engine-service/build.gradle
      plugins {
          id "org.asciidoctor.convert" version "1.5.9.2"
      }
      
      ext {
          snippetsDir = file('build/generated-snippets')
      }
      
      test {
          outputs.dir snippetsDir
      }
      
      asciidoctor {
          dependsOn test
          inputs.dir snippetsDir
          sourceDir = file('src/main/asciidoc')
          sources { include '*.adoc' }
          outputDir = file('build/docs')
      }
      
      jar {
          dependsOn asciidoctor
          from("${asciidoctor.outputDir}/html5"){
              into 'templates/docs'
          }
      }
      
      dependencies {        
          testCompile project(':avaj-core-mock')
          asciidoctor "org.springframework.restdocs:spring-restdocs-asciidoctor:${springRestDocs_version}"  
      }
  2. Check your controller

  3. It will be parsed to HTML file for documents when build packages.

    • avaj-engine-service/src/main/asciidoc/other-module-test.adoc
  4. Run

    • /run.sh to gradle bootRun to run WebApplication
  5. You can open documents !

mockmvc-and-restdoc's People

Contributors

souljungkim avatar

Stargazers

 avatar

Watchers

 avatar  avatar

mockmvc-and-restdoc's Issues

Some code needs to be delete as garbage code

generateConcatKeyParam - concatKey 분석

1. 대상코드

https://github.com/souljungkim/mockmvc-and-restdoc/blob/master/avaj-core-mock/src/main/groovy/com/avajjava/sample/TestHelper.groovy#L688-L732

2. 분석

  1. 결론:

    • 편의를 위해 기능을 산발적으로 개발하다가 정리를 못한 것을 또 다른 데서 사용하여 쓰레기코드가 떠돌아 다님.
    • Garbage 코드이므로 지워도 됨
  2. 취지:

    • Test코드의 Request Parameter의 가독성을 높일 수 있는 다양한 종류의 방안 연구`.
  3. (당시) 시도내용추정:

    • 다른 소스코드에서 급하게 따와서 뭔가 시도했다가 미처 지우지 못한 것으로 판단됨.
      https://github.com/avaj-java/common-man/blob/master/src/main/java/jaemisseo/man/VariableMan.groovy#L262-L278
    • 다음 두 가지는 동일하며 상황에 맞게 가독성이 높은 테스트코드의 Parameter를 사용하도록 한다.
       [
       	universe: [
       		solarSystem: [
       			earth: [
       				initialzer: "God",
       				population: 7_000_000_000L
       			],
       			mars: [
       				initialzer: "Elen",
       				population: 2_530L
       			],
       		],		
       	]
       ]
       [
       	"universe.solarSystem.earth.initialzer": "God",
       	"universe.solarSystem.earth.population": 7_000_000_000L,
       	"universe.solarSystem.mars.initialzer": "Elen",
       	"universe.solarSystem.mars.population": 2_530L,					
       ]
      • GET메소드의 경우 => 전자 to 후자의 구조로 자동 Parsing 후 MultiValueMap을 만들기
      • POST메소드의 경우 => 후자 to 전자의 구조로 자동 Parsing` 되도록 한 후 JSON으로 만들기
  4. (현재) 판단:

    • 테스트코드의 Request Body의 가독성을 위한 편리 기능을 제작하기 위한 취지임.
    • GET Method에서는 QueryString을 사용할 때는 식별할 수 있는 Unique한 원자성의 Data를 Key로서 Parameter를 사용하는 것이 일반적이므로 가용성이 적을 수 있음(?)
    • But !!! => SpringBoot 2.2.2 부터는 QueryString에 dot(.)을 이용하여 Object구조를 만들면 Spring알아서 Parsing해주는듯!? https://stackoverflow.com/a/55507251/12873116 => 잘 이용하면 GET에서도 위 2번에서의 후자의 구조로 역시 사용 가능할듯
    • POST Method에서는 일반적으로 사용하는 RequestBody를 이용한 다양한 Level의 VO구조에 적합한 JSON구조를 만들때 편리할 것이라고 판단.
    • 더나아가 위처럼 Map을 이용하는 것 말고 Groovy의 경우는 실제 사용하는 Object를 new하여 사용하는 것이 경우에 따라 가독성이 좋을 수 있음.
      • Groovy
         requestPost('/avaj/system/run',
         	bodyValues(new OneMind(
         		universe: new Universe(
         			solarSystem: new SolarSystem(
         				earth: new Earth(
         					initialzer: "God",
         					population: 7_000_000_000L,
         				),
         				mars: new Mars(
         					initialzer: "Elen",
         					population: 2_530L,
         				)
         			)
         		)
         	))
         )
      • Java (빌더패턴을 이용한달지.. 구릴수도..)
         requestPost("/avaj/system/run",
         	bodyValues(new OneMind().universe(
         		new Universe().solarSystem(
         			new SolarSystem()
         				.earth(
         					new Earth().initialzer("God").population(7_000_000_000L)								
         				)
         				.mars(
         					new Mars().initialzer("Elen").population(2_530L)
         				)							
         			)
         		)
         	))				
         );		

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.