[web-012] 一個標準的junit測試示例
阿新 • • 發佈:2018-12-15
1. 目錄結構
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── demo
│ │ │ └── test
│ │ │ └── HelloJunit.java
│ │ └── resources
│ └── test
│ └── java
│ └── TestHelloJunit.java
2. HelloJunit.java 內容如下
package com.demo.test; public class HelloJunit { public String hi(String name){ return "hi "+name; } public static void main(String[] args){ System.out.println("Hello, junit"); } }
3. TestHelloJunit.java內容如下
import com.demo.test.HelloJunit; import org.junit.*; import static junit.framework.Assert.*; import com.demo.test.*; public class TestHelloJunit { @Test public void testHello(){ HelloJunit h = new HelloJunit(); assertEquals(h.hi("bri"),"hi bri"); } }
4.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>com.brian.demo.test</groupId> <artifactId>mvntest</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> </dependencies> </project>
5. 在pom.xml目錄下執行 mvn clean test。