知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
<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>demo</groupId> <artifactId>junit4</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>junit4</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> <configuration> <useSystemClassLoader>false</useSystemClassLoader> </configuration> </plugin> </plugins> </build> <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>2.22.1</version> </plugin> </plugins> </reporting> </project>
生成報告
neo@MacBook-Pro ~/workspace/junit4 % mvn surefire-report:report-only
package cn.netkiller.api.test; import java.util.ArrayList; import java.util.List; import org.junit.experimental.categories.Category; import org.junit.runner.Description; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; public class ApiTestRunner extends BlockJUnit4ClassRunner { public ApiTestRunner(Class<?> class) throws InitializationError { super(class); } @Override protected Description describeChild(FrameworkMethod method) { Api api = method.getAnnotation(Api.class); if (api == null) { return super.describeChild(method); } List<String> list = new ArrayList<String>(); Class<?>[] categorys; if (method.getAnnotation(Category.class) != null) { categorys = method.getAnnotation(Category.class).value(); } else { categorys = this.getTestClass().getJavaClass().getAnnotation(Category.class).value(); } for (Class<?> category : categorys) { if (category.getSimpleName().equals("EnterpriseCategory")) { list.add("Enterprise"); } else if (category.getSimpleName().equals("ProfessionalCategory")) { list.add("Professional"); } } String tag = ""; if (list != null) { tag = String.join(",", list); } String name = String.format("%s - %s %s - %s", method.getName(), api.method(), api.uri(), tag); return Description.createTestDescription(getTestClass().getJavaClass(), name, method.getAnnotations()); } }
package cn.netkiller.api.test; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(value = RetentionPolicy.RUNTIME) @Target(value = { ElementType.METHOD }) public @interface Api { public ApiMethod method() default ApiMethod.GET; public String uri() default "/"; }
public interface ProfessionalCategory { String name = "Professional"; } public interface EnterpriseCategory { String name = "Enterprise"; }