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

How to use Java annotations and reflection to implement Junit4 calls

WBOY
Release: 2023-05-04 13:37:06
forward
939 people have browsed it

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"); }
Copy after login

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(); }
Copy after login

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");  } }
Copy after login

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();
Copy after login

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) { //过滤规则  }
Copy after login

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

If(method.getAnnotation(MyTag.class) != null)
Copy after login

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

If(method.getParameterCount()==0)
Copy after login

Run the method, the code is as follows:

method.invoke(obj)
Copy after login

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()); } } }
Copy after login

Run the code and the output is as follows:

logout
Copy after login

The test case name is: logout

login
Copy after login

The test case name is: login

info
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template