Yqj2065 is integrated with junit4.8.2 for the download and use of bluj. Therefore, analyze the source code of junit4.8.2.
JUnit is composedErich Gamma of one of gofAnd an open-source unit test framework compiled by Kent Beck. The main purpose of analyzing the source code of JUnit is to learnDesign Mode. JUnit is also a case study on how to cope with version upgrades and interface changes.
To read the source code, you must first understand the design requirements of the framework. If you have used JUnit, you should be familiar with unit testing requirements. Starting from a simple example, there is an application (the class to be tested) helloworld. To test it using junit4, we need to design a helloworldtest. Of course, we do not need to write code in bluej.
package myTest;public class HelloWorld { public double add(double m,double n){ return m+n; } public double add2(double m,double n){ return m+n; }}package myTest;import org.junit.Test;//@Testimport static org.junit.Assert.*;//assertEqualspublic class TestInJUnit4{ @Test public void add(){ HelloWorld h = new HelloWorld(); assertEquals(7.0, h.add(1, 2), 0.1); }}
The unit test class testinjunit4 is the code that is manually typed. The unit test class icon is dark green, and you can directly execute its @ test method.
JUnit will deal with the unit test class, and @ test and other labels/annotation will define a test. JUnit parses the runtime annotation through reflection,
A unit test case/a test case is a public void method.
package org.junit;import java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD})public @interface Test {/** * Default empty exception */static class None extends Throwable {private static final long serialVersionUID= 1L;private None() {}}/** * Optionally specify <code>expected</code>, a Throwable, to cause a test method to succeed iff * an exception of the specified class is thrown by the method. */Class<? extends Throwable> expected() default None.class;/** * Optionally specify <code>timeout</code> in milliseconds to cause a test method to fail if it * takes longer than that number of milliseconds.*/long timeout() default 0L; }
Org. JUnit. Ignore@ Target ({elementtype. method, elementtype. Type })
@ Before and @ After can only be identified by one method, replacing the setup and teardown methods in previous JUnit versions.
Org. JUnit. beforeclass@ Target (elementtype. Method)
Org. JUnit. Before@ Target (elementtype. Method)
Org. JUnit. afterclass@ Target (elementtype. Method)
Org. JUnit. After@ Target (elementtype. Method)
Org. JUnit. Runner. runwith run the test with the specified runner. The default runner is org. JUnit. Runners. junit4.
Org. JUnit. Runners. Suite. suiteclasses combines all the test classes to be run into a group/suite, one-time run to facilitate the test work.
Org. JUnit. Runners. parameterized. Parameters parameterization Test
Junit4.8.2 source code analysis-1 Description