springboot h2 allure automated t
1.提示错误
The class hierarchy was loaded from the following locations:
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: file:/C:/Users/Dev/.gradle/caches/modules-2/files-2.1/org.springframework/spring-webmvc/5.1.3.RELEASE/2ff9c2ddccaafd5b55756ceb4a65f02acb3b847a/spring-webmvc-5.1.3.RELEASE.jar
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport: file:/C:/Users/Dev/.gradle/caches/modules-2/files-2.1/org.springframework/spring-webmvc/5.1.3.RELEASE/2ff9c2ddccaafd5b55756ceb4a65f02acb3b847a/spring-webmvc-5.1.3.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
2.有问题的配置文件
plugins{
id'java'
id'org.springframework.boot' version'2.4.2' //还是有冲突
id'io.spring.dependency-management' version'1.0.11.RELEASE'
id'io.qameta.allure' version'2.8.1'
}
def allureVersion ="2.14.0"
compileJava.options.encoding ="UTF-8"
compileTestJava.options.encoding ="UTF-8"
configurations{
compileOnly{
extendsFrom annotationProcessor
}
testCompile{}
}
repositories{
mavenLocal()
mavenCentral()
maven{ url ='https://maven.aliyun.com/nexus/content/groups/public/' }
}
dependencies{
implementation'org.springframework.boot:spring-boot-starter'
implementation'org.springframework.boot:spring-boot-autoconfigure:2.2.1.RELEASE' //还是有冲突
implementation'org.springframework.boot:spring-boot-starter-web'
implementation'org.springframework.boot:spring-boot-starter-data-jpa'
implementation'org.springframework:spring-webmvc:5.1.3.RELEASE' //还是有冲突
implementation'javax.persistence:javax.persistence-api:2.2'
implementation'com.h2database:h2:1.4.200'
implementation'org.projectlombok:lombok'
testImplementation'io.qameta.allure:allure-junit4:2.12.0'
testImplementation'org.springframework.boot:spring-boot-starter-test'
testImplementation'javax.persistence:javax.persistence-api:2.2'
implementation'javax.xml.bind:jaxb-api:2.2.4'
compileOnly"io.qameta.allure:allure-java-commons:${allureVersion}"
testImplementation"io.qameta.allure:allure-java-commons:${allureVersion}"
}
group ='com.kenyada'
version ='1.0.1'
description ='springboot-autotest'
sourceCompatibility ='1.8'
allure{
autoconfigure =true
aspectjweaver =true
version = allureVersion
useJUnit5(){
version = allureVersion
}
}
test{
useJUnitPlatform()
testLogging{
outputs.upToDateWhen{false}
showStandardStreams =true
}
afterTest{ desc, result->
logger.quiet"Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
afterSuite{ desc, result->
if (!desc.parent)
println("${result.resultType} " +
"(${result.testCount} tests, " +
"${result.successfulTestCount} successes, " +
"${result.failedTestCount} failures, " +
"${result.skippedTestCount} skipped)")
}
}
//jar{
// manifest {
// attributes "Manifest-Version": 1.0.1 ,
// "Main-Class": "com.kenyada.demo.SpringAutomatedTestApplication"
// }
// from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
//}
还是有冲突
3.正确的配置文件
plugins{
id'java'
id'org.springframework.boot' version'2.5.2'
id'io.spring.dependency-management' version'1.0.11.RELEASE'
id'io.qameta.allure' version'2.8.1'
}
def allureVersion ="2.14.0"
compileJava.options.encoding ="UTF-8"
compileTestJava.options.encoding ="UTF-8"
configurations{
compileOnly{
extendsFrom annotationProcessor
}
testCompile{}
}
repositories{
mavenLocal()
mavenCentral()
maven{ url ='https://maven.aliyun.com/nexus/content/groups/public/' }
}
dependencies{
implementation'org.springframework.boot:spring-boot-starter'
implementation'org.springframework.boot:spring-boot-starter-web'
implementation'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly'com.h2database:h2'
testImplementation'org.springframework.boot:spring-boot-starter-test'
implementation'org.projectlombok:lombok'
testImplementation'io.qameta.allure:allure-junit4:2.12.0'
testImplementation'org.springframework.boot:spring-boot-starter-test'
compileOnly"io.qameta.allure:allure-java-commons:${allureVersion}"
testImplementation"io.qameta.allure:allure-java-commons:${allureVersion}"
}
group ='com.kenyada'
version ='1.0.1'
description ='springboot-autotest'
sourceCompatibility ='1.8'
allure{
autoconfigure =true
aspectjweaver =true
version = allureVersion
useJUnit5(){
version = allureVersion
}
}
test{
useJUnitPlatform()
testLogging{
outputs.upToDateWhen{false}
showStandardStreams =true
}
afterTest{ desc, result->
logger.quiet"Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
afterSuite{ desc, result->
if (!desc.parent)
println("${result.resultType} " +
"(${result.testCount} tests, " +
"${result.successfulTestCount} successes, " +
"${result.failedTestCount} failures, " +
"${result.skippedTestCount} skipped)")
}
}
//jar{
// manifest {
// attributes "Manifest-Version": 1.0.1 ,
// "Main-Class": "com.kenyada.demo.SpringAutomatedTestApplication"
// }
// from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
//}
4.默认会启动9090端口的web应用
4.1 启动服务
2021-08-03 14:58:26,528 [main] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [INFO] (TomcatWebServer.java:220)- Tomcat started on port(s): 9090 (http) with context path ''
2021-08-03 14:58:26,535 [main] [com.kenyada.demo.SpringAutomatedTestApplication] [INFO] (StartupInfoLogger.java:61)- Started SpringAutomatedTestApplication in 2.832 seconds (JVM running for 3.281)
检查是否启动4.2 查看启动接口
package com.kenyada.demo;
import com.kenyada.demo.model.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringAutomatedTestApplication.class)
@AutoConfigureMockMvc
@WebAppConfiguration
public class ApplicationMockTest {
protected static Loggerlogger = LoggerFactory.getLogger(ApplicationMockTest.class);
@Autowired
private MockMvcmockMvc;
@Autowired
private WebApplicationContextcontext;
@Before
public void setupMockMvc()throws Exception {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Before
public void testBefore() {
logger.info("testBefore...");
}
@After
public void testAfter() {
logger.info("testAfter...");
}
@Test
public void apiTest()throws Exception {
String url ="/hello";
String expectResult ="Hello,Automated test With SpringBoot";
MvcResult mvcResult =mockMvc.perform(MockMvcRequestBuilders.get(url))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(expectResult)).andReturn();
int status = mvcResult.getResponse().getStatus();
logger.info("return status=" + status);
String content = mvcResult.getResponse().getContentAsString();
logger.info("return content=" + content);
}
}
自己用idea 右键运行一下test测试用例
5.用命令提示
An exception occurred applying plugin request [id: 'org.springframework.boot', version: '2.5.2']
> Failed to apply plugin 'org.springframework.boot'.
> Spring Boot plugin requires Gradle 6.8.x, 6.9.x, or 7.x. The current version is Gradle 6.6.1
2.5.2版本是就版本。直接升级到2.5.3版本就可以支持gradle 7.1.1版本 ,allure 和gradle 7.1.1 冲突,还需要 降低版本
6.代码例子:
package com.kenyada.demo;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(classes = { ApplicationJavaTest.class })
@ExtendWith(SpringExtension.class)
public class ApplicationJavaTest {
protected static Loggerlogger = LoggerFactory.getLogger(ApplicationJavaTest.class);
@Test
public void testOne() {
logger.info("testOne test 1");
}
@Test
public void testTwo() {
logger.info("testTwo test 2");
assertEquals(1, 1);
}
@BeforeAll
public static void testBefore() {
logger.info("before");
}
@AfterAll
public static void testAfter() {
logger.info("after");
}
}
6.2.代码例子2
package com.kenyada.demo;
import java.net.URL;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationWebApiTest {
protected static Loggerlogger = LoggerFactory.getLogger(ApplicationWebApiTest.class);
@LocalServerPort
private int port;
@Autowired
private TestRestTemplaterestTemplate;
@Test
public void testUrlApi()throws Exception {
String url ="http://localhost:" +port+"/hello";
ResponseEntity response =this.restTemplate.getForEntity(url, String.class, "");
String result = response.getBody();
logger.info("return :result=" + result);
boolean expected =false;
if (result !=null && result.contains("Hello")) {
expected =true;
}
assertEquals(true, expected);
}
}
6.3代码例子3
package com.kenyada.demo;
import com.kenyada.demo.service.DemoService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = SpringAutomatedTestApplication.class)
public class ApplicationWebServiceTest {
protected static Loggerlogger = LoggerFactory.getLogger(ApplicationWebServiceTest.class);
@Autowired
private DemoServicedemoService;
/**
*
*
* @throws Exception
*/
@Test
public void testService()throws Exception {
String result =this.demoService.sayHello();
logger.info("return result=" + result);
assertNotNull(result);
String expected ="Hello,DemoService from service layer";
assertEquals(expected, result);
}
}