junit测试


junit javadoc ----> https://junit.org/junit4/javadoc/latest/index.html

1. Maven中junit测试

1.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>hello-mvn</artifactId>
  <version>1.0-SNAPSHOT</version>

	<dependencies>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>

	</dependencies>
	<!--配置Maven构建项目的参数设置, 设置jdk版本-->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugin</groupId>
				<!--插件的名称-->
				<artifactId>maven-compiler-plugin</artifactId>
				<!--插件的版本-->
				<version>3.8.1</version>
				<configuration>
					<!--配置maven代码在jdk1.8上编译-->
					<source>1.8</source>
					<!--程序在jdk1.8上运行-->
					<targe>1.8</target>
				</configuration>
			</plugins>
		</plugins>
	</build>
</project>

1.2 编写待测的类org.example.HelloMaven

package org.example;
public class HelloMaven {
    public static int add(int a, int b) {
        return a + b;
    }

    public static float mutiple(float a, float b) {
        return a / b;
    }
}

1.3 编写junit测试代码

package org.example;

import org.junit.Test;
import static org.junit.Assert.*;

public class HelloMavenTest {

    @Test
    public void testAdd() {
		System.out.println("-----testing add method-----");
        HelloMaven hm = new HelloMaven();
        assertEquals(30, hm.add(10, 20));
    }
	
	@Test
    public void testMutiple() {
		System.out.println("-----testing mutiple method-----");
        HelloMaven hm = new HelloMaven();
        assertEquals(2, hm.mutiple(1, 2), 0.001);
    }
}

E:\IdeaProjects\HelloMaven>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< org.example:hello-mvn >------------------------
[INFO] Building hello-mvn 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-mvn ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\IdeaProjects\HelloMaven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-mvn ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-mvn ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-mvn ---
[INFO] Surefire report directory: E:\IdeaProjects\HelloMaven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.example.HelloMavenTest
-----testing add method-----
-----testing mutiple method-----
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.067 sec <<< FAILURE!
testMutiple(org.example.HelloMavenTest)  Time elapsed: 0.005 sec  <<< FAILURE!
java.lang.AssertionError: expected:<2.0> but was:<0.5>
        at org.junit.Assert.fail(Assert.java:93)
        at org.junit.Assert.failNotEquals(Assert.java:647)
        at org.junit.Assert.assertEquals(Assert.java:443)
        at org.junit.Assert.assertEquals(Assert.java:512)
        at org.example.HelloMavenTest.testMutiple(HelloMavenTest.java:19)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)


Results :

Failed tests:   testMutiple(org.example.HelloMavenTest): expected:<2.0> but was:<0.5>

Tests run: 2, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.566 s
[INFO] Finished at: 2021-02-16T12:42:29+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project hell
o-mvn: There are test failures.
[ERROR]
[ERROR] Please refer to E:\IdeaProjects\HelloMaven\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

1.4 结果分析

从测试结果testMutiple(org.example.HelloMavenTest): expected:<2.0> but was:<0.5>看出,期望是2.0,但是得到了0.5,说明mutiple()方法有错误。

2. IntelliJ IDEA中junit测试

2.1 配置Runner中的VM OptionsJRE

VM Options:-DarchetypeCatalog=internal。idea每次创建maven工程,默认都会下载7M左右的模板文件,比较慢,设置-DarchetypeCatalog=internal,告诉idea使用本地的,不用下载。
JRE:1.8

2.2 编写junit测试类

  1. 在需要测试的类上alt+enter
    图片名称

  2. Create Test,选择需要测试的方法,点击OK
    图片名称

  3. 编写测试代码,在类上面点击可以运行所有方法,在方法上点击只运行这一个方法。
    图片名称

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值