Use of JUnit's Rule, and use of JUnit's Rule
Brief Introduction to the simple usage of JUnit Rule. Prepare for source code analysis on JUnit.
Rule is a annotation used to test a unit class. For example, if MyTest defines a field, the field must be public, not static, and a subtypeOrg. junit. rules. MethodRule.
package org.junit;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)public @interface Rule {}
Kent Beck once wrote an article <Interceptors in JUnit>. Although it is called Rule, it still plays the role of Interceptor/Interceptor-that is, adding some useful code before and after running the test.
[Note: Beginning with JUnit4.9, MethodRule is replaced by deprecated and TestRule. The unique method defined by the MethodRule interface:
Statement apply (Statement base, FrameworkMethod method, Object target );
TestRule:
Statement apply (Statement base, Description description );
] (Seriously affecting yqj2065 reading the source code 4.8.2)
① Prepare a Statement for MyRule first
package myTest.rule;import static tool.Print.*;//pln(Object)import org.junit.runners.model.Statement;public class MyStatement extends Statement { private final Statement base; public MyStatement( Statement base ) { this.base = base; } @Override public void evaluate() throws Throwable { pln( "before...sth..sth" ); try { base.evaluate(); } finally { pln( "after...sth..sth" ); } }}
② Define MyRule
package myTest.rule;//import org.junit.runner .Description;import org.junit.rules.MethodRule;import org.junit.runners.model.Statement;import org.junit.runners.model.FrameworkMethod;public class MyRule implements MethodRule { @Override public Statement apply( Statement base, FrameworkMethod method, Object target ) { return new MyStatement( base ); }}
③ Use Rule in the test unit class MyTest.
package myTest.rule;import org.junit.Rule;import org.junit.Test;public class MyTest { @Rule public MyRule myRule = new MyRule(); @Test public void testCase() { System.out.println( "testCase()..." ); }}
Output of running testCase:
Before... ......
TestCase ()...
After...
How does JUnit test perform Java Static Method Testing?
Test only allows you to use the void method. If your other method is static and has a returned value, you can completely package
In java, how should Junit be used?
JUnit
JUnit is a regression testing framework (regression testing framework) written by Erich Gamma and Kent Beck ). Junit testing is a programmer test, called a white-box test, because the programmer knows How the tested software completes its functionality and What it does. Junit is a framework that inherits the TestCase class and can be automatically tested using Junit.
More JUnit Information
Cactus
Cactus is a simple testing framework based on the JUnit framework. It is used to unit test the Java code of the server. The main goal of the Cactus framework is to be able to unit test the server's Java methods using Servlet objects, such as HttpServletRequest, HttpServletResponse, and HttpSession.
More Cactus Information
Abbot
Abbot is a framework used to test Java GUIs. With simple XML-based scripts or Java code, you can start a GUI.
More Abbot Information
JUnitPerf
Junitperf is actually a decorator of junit. By writing unit tests for junitperf, we can also automate the testing process.
More information about JUnitPerf
DbUnit
DbUnit is an extension of JUnit for database-driven projects. In addition to some common functions, it can place your database in a state between testing cycles.
More DbUnit Information
Mockrunner
Mockrunner is used in the J2EE environment for unit testing of applications. It not only supports Struts actions, servlets, filters and tag classes, but also a JDBC and a JMS test framework that can be used to test EJB-based applications.
More Mockrunner Information
DBMonster
DBMonster is a stress testing tool that generates random data to test the SQL database.
More DBMonster Information
MockEJB
MockEJB is a lightweight framework that can run and test EJB without an EJB container.
More MockEJB Information
StrutsTestCase
StrutsTestCase is an extension of the Junit TestCase class and provides code testing based on the Struts framework. StrutsTestCase also provides the Mock object method and Cactus Method for running Struts ActionServlet. You can test it by running the servlet engine. Because StrutsTestCase uses the ActionServlet controller to test your code, you can not only test the implementation of the Action object, but also test mappings, from beans, and forwards declarations. StrutsTestCase does not start the servlet container to test the struts application (out-of-container test). However, unlike EasyMock, EasyMock provides an API for creating a Mock object, strutsTest is a Mock object testing framework specifically responsible for testing Struts applications.
More information about StrutsTestCase
JFCUnit
JFCUnit enables you to write test examples for Java offset applications. It provides support for obtaining handles from a code-Opened Window, locating a widget at a component level, and initiating events in a widget (for example, pressing a button) it also provides support for processing part tests in thread-safe mode.
More information about JFCUnit
JTestCase
JTestCase uses XML files to organize multi-test case data, declare conditions (operations and expected results), and provide a set of easy-to-use methods to retrieve... the remaining full text>