spring testng

1.spring testng gradle 编译

build.gradle

Plus the red part, Ali cloud configuration, speed up compilation

buildscript {
   repositories {
      //mavenCentral()
           maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}

   }
   dependencies {
      classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.1.9.RELEASE'
   }
}

plugins {
   id "org.asciidoctor.convert" version "1.5.9.2"
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'eclipse'
apply plugin: 'io.spring.dependency-management'

repositories {
   mavenLocal()
   maven { url 'https://repo.spring.io/libs-snapshot' }
   //mavenCentral()
           maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
}

group = 'com.example'

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
   snippetsDir = file('build/generated-snippets')
}

ext['spring-restdocs.version'] = '2.0.5.BUILD-SNAPSHOT'

dependencies {
   asciidoctor 'org.springframework.restdocs:spring-restdocs-asciidoctor'
   compile 'org.springframework.boot:spring-boot-starter-web'
   testCompile('org.springframework.boot:spring-boot-starter-test') {
      exclude group: 'junit', module: 'junit;'
   }
   testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'
   testCompile 'org.testng:testng:6.9.10'
}

test {
   useTestNG()
   outputs.dir snippetsDir
}

asciidoctor {
   inputs.dir snippetsDir
   dependsOn test
}

bootJar {
   dependsOn asciidoctor
   from ("${asciidoctor.outputDir}/html5") {
      into 'static/docs'
   }
}

eclipseJdt.onlyIf { false }
cleanEclipseJdt.onlyIf { false }

allprojects{
   repositories {
      //mavenCentral()
           maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}

   }
}

2. Source Code

 

package com.example.testng;

import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.lang.reflect.Method;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.restdocs.ManualRestDocumentation;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@SpringBootTest
public class SampleTestNgApplicationTests extends AbstractTestNGSpringContextTests {

   private final ManualRestDocumentation restDocumentation = new ManualRestDocumentation();

   @Autowired
   private WebApplicationContext context;

   private MockMvc mockMvc;

   @BeforeMethod
   public void setUp(Method method) {
      this.mockMvc = MockMvcBuilders.webAppContextSetup(context)
            .apply(documentationConfiguration(this.restDocumentation)).build();
      this.restDocumentation.beforeTest(getClass(), method.getName());
   }

   @AfterMethod
   public void tearDown() {
      this.restDocumentation.afterTest();
   }

   @Test
   public void sample() throws Exception {
      this.mockMvc.perform(get("/"))
            .andExpect(status().isOk())
            .andDo(document("sample"));
   }

}

 

3. compile time, performed a test report generation testng

State after running

4. Run results

 

Published 301 original articles · won praise 16 · views 30000 +

Guess you like

Origin blog.csdn.net/keny88888/article/details/105300153