- Writing target class source code
Package com.mengdd.junit;
public class Calculator
{
public int Add (int a, int b)
{
return a + B;
}
public int subtract (int a, int b)
{
return a-B;
}
public int multiply (int a, int b)
{
return a * b;
}
public int divide (int a, int b)
{
return a/b;
}
}
- Add a JUnit Library
Then in order to use JUnit, you need to join the library:
Right-click on the project properties-> left Java Build path-> tag libraries->add Library ...
The popup dialog selects junit, then Next, and then junit 3 or junit 4.
This example selects JUnit 3.
- To create a test class
Here are a few things to keep in mind:
1. best Practices for using JUnit: source code and test code need to be separated.
Therefore, you can create a new source foldernamed Test, which is used to store the code of the testing class. This allows the program to be tested when the program is released and can be discarded.
However, the class files compiled by the classes in both folders will be in the same bin folder.
2. the classes that test the class and the target source code should be under the same package, i.e. their package names should be the same. in this way, you do not have to import the package that contains the source code in your test class, because they are located under the same package.
3. naming rules for test classes:
Add test before or after the class name to be tested.
After this step is complete, the project directory is as follows:
- Test Class Code writing
The test class must inherit from the TestCase class.
TestCase Document Description :
Public abstract class TestCase
Extends Assert
Implements Test
A test case defines the fixture to run multiple tests. To define a test case
1) Implement a subclass of TestCase
2) Define instance variables that store the state of the fixture
3) Initialize the fixture state by overriding setup
4) clean-up after a test by overriding TearDown.
Each test runs in it own fixture so there can be no side effects among test runs.
Requirements for methods in the test class:
In JUnit 3.8 , the test method needs to meet the following principles:
1.public the.
2.void the.
3. No method parameters.
4. the method name must begin with test . (it finds all the methods by reflection and then finds The method that starts with test).
It is important to maintain full independence between Test case and not allow any dependencies.
Removing some of the methods will have no effect on other methods.
We cannot rely on the execution order of the test methods.
In summary, write the following code:
Package com.mengdd.junit;
Import Junit.framework.Assert;
Import Junit.framework.TestCase;
public class Calculatortest extends TestCase
{
public void Testadd ()
{
Calculator Calculator = new Calculator ();
int result = Calculator.add (1, 2);
The return result of the Judgment method
Assert.assertequals (3, result);//The first parameter is expected, the second argument is the value to validate
}
public void Testsubtract ()
{
Calculator Calculator = new Calculator ();
int result = Calculator.subtract (1, 2);
The return result of the Judgment method
Assert.assertequals ( -1, result);//The first parameter is expected, the second argument is the value to validate
}
public void testmultiply ()
{
Calculator Calculator = new Calculator ();
int result = Calculator.multiply (2, 3);
The return result of the Judgment method
Assert.assertequals (6, result);//The first parameter is expected, the second argument is the value to validate
}
public void Testdivide ()
{
Calculator Calculator = new Calculator ();
int result = Calculator.divide (12, 3);
The return result of the Judgment method
Assert.assertequals (4, result);//The first parameter is expected, the second argument is the value to validate
}
}
Run: Right-Select the class,run As->junit Test
JUnit slogan: Keep theBar Green to Keep the code clean.
- Code refactoring: using the setUp () method
There is a principle:DRY(Don't Repeat yourself)
So refactor the code and place the duplicated part of the generated object in the setUp () method.
(When overridden, the protected becomes public, and it is no problem to extend the scope of access when inheriting.) First, test the test with a method:
Add the following code to the calculatortest class:
@Override
public void SetUp () throws Exception
{
System.out.println ("Set up");
}
@Override
public void TearDown () throws Exception
{
System.out.println ("Tear Down");
}
After running again, find the output in the Console as follows:
Describes the two methods that were executed several times.
The setUp ()is executed before each test case, andTearDown () executesafter each test case executes .
That is, for each test case, the order of execution is:
1.setUp ()
2.testXXX ()
3.tearDown ()
Refactoring: Generate objects using member variables (in order to be used in each method), place the statements of the generated objects in setUp () , and note that each test case will generate new objects.
After refactoring the code is as follows:
Package com.mengdd.junit;
Import Junit.framework.Assert;
Import Junit.framework.TestCase;
public class Calculatortest extends TestCase
{
Private Calculator Calculator = null;
@Override
public void SetUp () throws Exception
{
System.out.println ("Set up");
To generate an instance of a member variable
Calculator = new Calculator ();
SYSTEM.OUT.PRINTLN (Calculator);
}
@Override
public void TearDown () throws Exception
{
System.out.println ("Tear Down");
}
public void Testadd ()
{
int result = Calculator.add (1, 2);
The return result of the Judgment method
Assert.assertequals (3, result);//The first parameter is expected, the second argument is the value to validate
}
public void Testsubtract ()
{
int result = Calculator.subtract (1, 2);
The return result of the Judgment method
Assert.assertequals ( -1, result);//The first parameter is expected, the second argument is the value to validate
}
public void testmultiply ()
{
int result = Calculator.multiply (2, 3);
The return result of the Judgment method
Assert.assertequals (6, result);//The first parameter is expected, the second argument is the value to validate
}
public void Testdivide ()
{
int result = Calculator.divide (12, 3);
The return result of the Judgment method
Assert.assertequals (4, result);//The first parameter is expected, the second argument is the value to validate
}
}
After running the console output:
Indicates that each test method has a call to the setUp () and TearDown () methods before and after each, so each generation is a new object and there is no interference between the methods .
Https://github.com/gaodejian/gaodejian/commit/a953bfbe1f3f533f60a6e1bae61e9a49275778eb
201303014111 High-Tak building