Home  >  Article  >  Java  >  How to use Java annotations and reflection to implement Junit4 calls

How to use Java annotations and reflection to implement Junit4 calls

WBOY
WBOYforward
2023-05-04 13:37:06931browse

Instance requirements

Requirements: Simulate the use case structure in Junit4 and customize a label as the identification of the test case.

The annotation @Test in Junit4 represents a test case. The essence of each test case is a method in the test class, that is:

@Test public void test() { fail("Not yet implemented"); }

Specific requirements:

Test class It is the default construction method

Use the label MyTag as the identifier of whether the method is a use case

The method as the use case must be parameterless

Write a method runCase(String pkgName), To enable it to complete the call of test cases in the specified class

Annotation design

First you need to define an annotation MyTag, the code is as follows:

import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.annotation.ElementType; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyTag{ String name(); }

Test case design

Simulate the junit4 framework and write a test class, in which login, info, and logout are parameterless methods, and test is a parameterized method

public class TestTag{ @MyTag(name="case1") public void login(){ System.out.println("login"); } @MyTag(name="case2") public void info(){ System.out.println("info");  } @MyTag(name="case3") public void logout(){ System.out.println("logout"); } @MyTag(name="case4") public void test(String p){ System.out.println("logout");  } }

The design idea of ​​the running class

Because the test class It is the default construction method, so use the following API to complete class instantiation

Class<?> clazz = Class.forName(pkgName) Object obj = clazz.newInstance();

Because there are many methods in the test class, we need to obtain all methods and filter them according to the rules. The code is as follows:

Method[] methods = clazz.getMethods(); for (Method method : methods) { //过滤规则  }

To determine whether the tag of the method is MyTag, the code is as follows:

If(method.getAnnotation(MyTag.class) != null)

To determine whether the method has no parameters, the code is as follows:

If(method.getParameterCount()==0)

Run the method, the code is as follows:

method.invoke(obj)

Complete code

public static void runCase(String pkgName) throws IllegalAccessException,IllegalArgumentException, InvocationTargetException, InstantiationException,ClassNotFoundException {  Class<?> clazz = Class.forName(pkgName);  Object obj = clazz.newInstance();  Method[] methods = clazz.getMethods(); for (Method method : methods) {        if(method.getAnnotation(MyTag.class) != null&& method.getParameterCount()==0) {                                 method.invoke(obj); //调用方法                System.out.println("测试用例名称是:"+method.getName()); } } }

Run the code and the output is as follows:

logout

The test case name is: logout

login

The test case name is: login

info

The test case name is :info

The above is the detailed content of How to use Java annotations and reflection to implement Junit4 calls. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete