The JUNIT4 is the biggest improvement ever made by the JUnit framework, whose main goal is to simplify the writing of test cases using Java5 's annotation features.
Let's briefly explain what annotation is, and the word is generally translated into meta data. What the meta data is. Meta data is the data that describes the data. That is, it can be used in Java to decorate class names, method names, and variable names like public, static, and other keywords. The role of the modifier describes what this data is for, almost as public as it is publicly described. You can see the core Java2 to get a specific idea. No more nonsense, go straight to the point.
Let's take a look at how we write a unit test in JUnit 3. For example, one of the following classes:
public class Addoperation {
public int Add (int x,int y) {
return x+ y;
}
}
We're going to test the Add method, and we write unit tests like this:
Import Junit.framework.TestCase;
Import static org.junit.assert.*;
public class Addoperationtest extends testcase{
public void SetUp () throws Exception {
}
public void Teardown () throws Exception {
}
public void Testadd () {
System.out.println ("add");
int x = 0;
int y = 0;
addoperation instance = new Addoperation ();
int expresult = 0;
int result = Instance.add (x, y);
Assertequals (Expresult, result);
}
}
You can see that the class above uses the static import in JDK5, this is relatively simple, as long as you add the static keyword after the import keyword, you can import the static variables and methods of the following class into the class, the call and the way you call your own.
We can see the above unit test some of the more overbearing places, the performance in:
1. unit test class must inherit from TestCase.
2. The method to be tested must begin with test.
If the above unit test is written in JUnit 4, it will not be so complicated. The code is as follows:
Import Junit.framework.TestCase;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;
Import static org.junit.assert.*;
/**
*
* @author Bean
*/
public class Addoperationtest extends testcase{
Public Addoperationtest () {
}
@Before
public void SetUp () throws Exception {
}
@After
public void Teardown () throws Exception {
}
@Test
public void Add () {
System.out.println ("add");
int x = 0;
int y = 0;
addoperation instance = new Addoperation ();
int expresult = 0;
int result = Instance.add (x, y);
Assertequals (Expresult, result);
}
}
We can see that the adoption of annotation JUnit has not been overbearing enough to require you to inherit from the TestCase, and the test method does not have to start with test, as long as the @test metadata to describe it.
As you can see from the example above, some other metadata is also introduced in JUnit 4, as described below:
@Before:
Methods that use this metadata are executed once before each test method executes.
@After:
Methods that use this metadata are executed once after each test method has been executed.
Note: @Before and @after can only be labeled with one method. This is equivalent to replacing the setup and Teardown methods in the previous version of JUnit, and of course you can continue to call that name, but JUnit will not be bossy enough to ask you to do so.
@Test (Expected=*.class)
Before JUnit4.0, for the wrong test, we can only produce an error by fail, and test in the try block Asserttrue (true). Now, by @test the expected attribute in the metadata. The value of the expected property is the type of an exception
@Test (timeout= XXX):
The metadata is passed in a time (in milliseconds) to the test method,
If the test method does not run out within the time set, the test fails.
@ignore:
The test method for this metadata token is ignored in the test. Use this label to mark this method when the test method has not been implemented, or if the test method is outdated, or under certain conditions to test the method (for example, if a database join is required and the database is not connected at the time of the local test). At the same time, you can pass a string argument to the tag to show why the test method is ignored. For example, @lgnore ("This method is not yet implemented"), when executed, only reports that the method is not implemented, but does not run the test method.
The Assert methods in JUnit are all placed in the Assert class, summarizing the classification of the Assert methods in the JUnit class.
1.asserttrue/false ([String message,]boolean condition);
Determines whether a condition is true or false. Feel this is the best use, do not have to write down so many method names.
2.fail ([String message,]);
failure, you can have a message, you can have no news.
3.assertEquals ([String message,]object expected,object Actual);
to determine if equality, you can specify an output error message.
The first argument is expected, and the second argument is the actual value.
This method has a variety of implementations for each variable. Basically the same in JDK1.5.
But what is needed is the value of one more delta, float and double, which may be the margin of error, not sure what this word means, Khan one.
4.assertnotnull/null ([String message,]object obj);
interpreting whether an object is Non-null (Non-empty).
5.assertsame/notsame ([String message,]object expected,object Actual);
determine whether two objects point to the same object. Look at the memory address.
7.failnotsame/failnotequals (String message, object expected, object actual)
Output an error message when it does not point to the same memory address or is not equal.
Note that information is required and that the output is formatted.
This article is from: Http://hi.baidu.com/beanchx