java 单体测试_在Spring boot的项目中使用Junit进行单体测试

朱梓
2023-12-01

使用junit或者testng可以进行单体测试,这篇文章简单说明一下如何在spring boot的项目中使用junit进行单体测试。

pom设定

pom中需要添加spring-boot-starter-test

org.springframework.boot

spring-boot-starter-test

test

确认依赖

使用java进行unit测试至少需要junit之类的测试框架,另外spring boot相关的测试还应该有一些mock相关的依赖,这个spring-boot-starter-test到底包含什么,可以使用maven dependency来进行确认一下。

使用命令:mvn dependency:tree

结果信息:

[info] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ springbootdemo ---

[info] com.liumiaocn:springbootdemo:jar:0.0.1-snapshot

[info] +- org.springframework.boot:spring-boot-starter-web:jar:2.0.6.release:compile

[info] | +- org.springframework.boot:spring-boot-starter:jar:2.0.6.release:compile

[info] | | +- org.springframework.boot:spring-boot:jar:2.0.6.release:compile

[info] | | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.6.release:compile

[info] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.6.release:compile

[info] | | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile

[info] | | | | \- ch.qos.logback:logback-core:jar:1.2.3:compile

[info] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile

[info] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile

[info] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile

[info] | | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile

[info] | | \- org.yaml:snakeyaml:jar:1.19:runtime

[info] | +- org.springframework.boot:spring-boot-starter-json:jar:2.0.6.release:compile

[info] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.7:compile

[info] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile

[info] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.7:compile

[info] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.7:compile

[info] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.7:compile

[info] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.7:compile

[info] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.6.release:compile

[info] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.34:compile

[info] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.34:compile

[info] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.34:compile

[info] | +- org.hibernate.validator:hibernate-validator:jar:6.0.13.final:compile

[info] | | +- javax.validation:validation-api:jar:2.0.1.final:compile

[info] | | +- org.jboss.logging:jboss-logging:jar:3.3.2.final:compile

[info] | | \- com.fasterxml:classmate:jar:1.3.4:compile

[info] | +- org.springframework:spring-web:jar:5.0.10.release:compile

[info] | | \- org.springframework:spring-beans:jar:5.0.10.release:compile

[info] | \- org.springframework:spring-webmvc:jar:5.0.10.release:compile

[info] | +- org.springframework:spring-aop:jar:5.0.10.release:compile

[info] | +- org.springframework:spring-context:jar:5.0.10.release:compile

[info] | \- org.springframework:spring-expression:jar:5.0.10.release:compile

[info] \- org.springframework.boot:spring-boot-starter-test:jar:2.0.6.release:test

[info] +- org.springframework.boot:spring-boot-test:jar:2.0.6.release:test

[info] +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.0.6.release:test

[info] +- com.jayway.jsonpath:json-path:jar:2.4.0:test

[info] | +- net.minidev:json-smart:jar:2.3:test

[info] | | \- net.minidev:accessors-smart:jar:1.2:test

[info] | | \- org.ow2.asm:asm:jar:5.0.4:test

[info] | \- org.slf4j:slf4j-api:jar:1.7.25:compile

[info] +- junit:junit:jar:4.12:test

[info] +- org.assertj:assertj-core:jar:3.9.1:test

[info] +- org.mockito:mockito-core:jar:2.15.0:test

[info] | +- net.bytebuddy:byte-buddy:jar:1.7.11:test

[info] | +- net.bytebuddy:byte-buddy-agent:jar:1.7.11:test

[info] | \- org.objenesis:objenesis:jar:2.6:test

[info] +- org.hamcrest:hamcrest-core:jar:1.3:test

[info] +- org.hamcrest:hamcrest-library:jar:1.3:test

[info] +- org.skyscreamer:jsonassert:jar:1.5.0:test

[info] | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test

[info] +- org.springframework:spring-core:jar:5.0.10.release:compile

[info] | \- org.springframework:spring-jcl:jar:5.0.10.release:compile

[info] +- org.springframework:spring-test:jar:5.0.10.release:test

[info] \- org.xmlunit:xmlunit-core:jar:2.5.1:test

在org.springframework.boot:spring-boot-starter-test:jar:2.0.6.release:test下面我们看到了junit:junit:jar:4.12:test以及org.mockito:mockito-core:jar:2.15.0:test的信息,这是spring boot已经整理完毕的内容,我们只需要写testcase即可。

创建测试目录

根据惯例创建测试目录如下:

liumiaocn:src liumiao$ ls

main test

liumiaocn:src liumiao$ find . -type d

.

./test

./test/java

./test/java/com

./test/java/com/liumiaocn

./test/java/com/liumiaocn/springbootdemo

./main

./main/resources

./main/java

./main/java/com

./main/java/com/liumiaocn

./main/java/com/liumiaocn/springbootdemo

liumiaocn:src liumiao$

创建测试用例

代码示例

liumiaocn:src liumiao$ cat ./test/java/com/liumiaocn/springbootdemo/springbootdemoapplicationtests.java

package com.liumiaocn.springbootdemo;

import org.junit.test;

import org.junit.runner.runwith;

import org.springframework.boot.test.context.springboottest;

import org.springframework.test.context.junit4.springrunner;

@runwith(springrunner.class)

@springboottest

public class springbootdemoapplicationtests {

@test

public void contextloads() {

}

}

liumiaocn:src liumiao$

代码说明

coc:根据惯例进行定义测试类的名称

springboottest注解:springboottest是1.4之后引入的一个注解,使得springboot的测试变得更加方便

runwith注解:使用了此注解的情况下,junit会调用runwith中所指定的类。不同的框架提供相应的runner用于测试,比如junit自己的junit4.class,在比如spring的springjunit4classrunner或者springrunner,都可与之结合使用。

test注解:junit的常用注解之一,用于定义测试方法,不再赘述。

执行测试

命令: mvn test

部分执行内容如下:

[info] results:

[info]

[info] tests run: 1, failures: 0, errors: 0, skipped: 0

在实施的结果中可以看到运行起来的测试用例为1个,没有失败/跳过/出错的。

小结

通过spring-boot-starter-test的引入,在springboot中已经做好所有单元测试的准备,根据惯例的方式设定测试目录和文件名称,结合使用springboottest等注解,可以使用junit对springboot的应用进行测试。最后使用mvn test即可运行相关的测试用例并可确认结果,后面将会进一步说明如何与sonarqube以及jacoco等进行结合确认代码扫描等。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对萬仟网的支持。如果你想了解更多相关内容请查看下面相关链接

希望与广大网友互动??

点此进行留言吧!

 类似资料: