In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
In this issue, Xiaobian will bring you about how to use the test tool JUnit 4. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
introduced
JUnit 4.x takes advantage of Java 5 features (Annotation), making testing easier than version 3.x, JUnit 4.x is not a simple upgrade of the old version, it is a brand new framework, the package structure of the entire framework has been completely changed, but version 4.x is still very compatible with the old version of the test case.
use
Let's start with something real and see how it's used in the code. The rest can wait.
download
Download the JUnit 4.8.1.jar package (download address provided here)
join the project
Add the junit4.8.1.jar file to the classpath of the project.
contrast
Before we start with the code, let's look at the differences between JUnit 4 and JUnit 3 to see what JUnit 4 simplifies.
demo code
[java] view plain copy
package com.tgb;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
public class TestWordDealUtil {
//Test the normal operation of wordFormat4DB
@Test
public void testWordFarmat4DBNormal() {
String target = "employeeInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info", result);
}
//Test null processing
@Test(expected=NullPointerException.class)
public void testWordFormat4DBNull() {
String target = null;
String result = WordDealUtil.wordFormat4DB(target);
assertNull(result);
}
//Test empty string processing
@Test
public void testWordFormat4DBEmpty() {
String target = "";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("", result);
}
//Test when the first letter is capitalized
//@Ignore
@Test
public void testWordFormat4DBBegin() {
String target = "EmployeeInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("_employee_info", result);
}
//Test when the last letter is capitalized
@Test
public void testWordFormat4DBEnd() {
String target = "employeeInfoA";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info_a", result);
}
//Test multiple consecutive letters in capital letters
@Test
public void testWordFormat4DBTogether() {
String target = "employeeAInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_a_info", result);
}
}
As can be seen from the figure, TestWordDealUtil test class, there are 6 test methods, of which 5 test methods have passed, the other threw a NullPointerException (null pointer) exception, it should be noted that here is not a unit test failure (Failure), but a test error (Error). So, what's the difference between the two? The difference between the two is discussed below.
JUnit divides test failures into two categories: Failure and Error. Failure is generally caused by the failure of the assertion method used by the unit test, which indicates that a problem (bug in the program) was found at the test point; Error is caused by a code exception, which is a discovery outside the purpose of the test. It may result from an error in the test code itself (that is, there is a problem with the written test code) or it may be a hidden bug in the tested code. However, it is usually the first case.
In-depth common comments
@Before
Initialize methods, code that must be executed before any of the test methods can be executed. Compare JUnit 3, which has the same functionality as the setUp () method. In this annotated method, you can do some preparatory work, such as initializing objects, opening network connections, and so on.
@After
Frees up resources and any finishing work that needs to be done after any test method is executed. Compared to JUnit 3, the tearDown () method has the same functionality.
@Test
The test method indicates that this is a test method. This will be done automatically in JUnit. There are also requirements for the declaration of methods: the name can be arbitrary, there are no restrictions, but the return value must be void, and there must be no parameters. If these rules are violated, an exception is thrown at runtime. However, in order to cultivate a good programming habit, we usually add test to the method name of the test, for example: testAdd ().
At the same time, the Annotation (@Test) can also test expected exceptions and timeout times, such as @Test (timeout=100), we set a test function execution time, beyond this time (100 milliseconds), they will be forcibly terminated by the system, and the system will also report to you that the function ended because of timeout, so you can find these bugs. Also, it can test for expected exceptions, such as the null pointer exception we just had: @Test(expected=NullPointerException.class). Let's take a look at the test results.
@Ignore
The test methods ignored are labeled as "some methods are not complete, we will not participate in this test"; this way, the test results will indicate that you have several tests ignored, rather than failed. Once you've completed the function, just delete the @Ignore annotation and you're ready for normal testing. Of course, this @Ignore annotation still makes a lot of sense to people like me who have OCD. Every time you see a red bar (failure), you feel uncomfortable and unbearable (unless the purpose of the test is to make it fail). Of course, the same is true for code, and I can't stand messy code. So, I suggest that everyone write beautiful code. Everyone loves to read your code. Sigh, people with obsessive compulsive disorder can't afford to be hurt!
@BeforeClass
For all tests, that is, for the entire test class, the method annotated by it is executed only once before all test methods are executed. Note, of course, that the modifier must be public static void xxxx ; this Annotation is a new feature in JUnit 4.
@AfterClass
For all tests, that is, for the entire test class, the method annotated by it is executed only once after all test methods have been executed. Of course, it should be noted that the modifier must also be public static void xxxx ; this Annotation is also a new feature of JUnit 4, and is paired with @BeforeClass.
execution order
So, in JUnit 4, the unit test cases are executed in the following order:
Each test method is called in the following order:
specification
Finally, there are specifications for testing, which are summarized from programming rules and everyday practices by the big names. As descendants of us, while enjoying the cool under the tree, we must abide by these rules to make the tree grow stronger.
Unit test code should be located under a separate Source Folder
This Folder Source is usually test, which makes it easy to manage business code and test code. In fact, this specification has already been done on the project management tool Maven. When we write our own code, we can pay attention to it.
The test class should be in the same package as the class under test
Easy to manage, while reducing the trouble of introducing test classes.
Choose meaningful test method names
Whether it is JUnit 4 or JUnit 3, the unit test method name needs to use test[summary description], such as public void testDivideDivisorIsZero(), it is easy to know the meaning of the test method.
Independence of preservation tests
Each unit test must be run independently of all other unit tests because unit tests need to be able to run in any order.
Ignore (@Ignore) or throw fail for temporarily unimplemented test code
In JUnit 4, you can use the annotation @Ignore on test methods. In JUnit 3, you can use fail("test method not implemented") in an unimplemented test method; to tell that the failure is because the test method is not implemented.
Give the reason for failure when invoking an assert method
When using assertion methods, use an API with a message parameter and a description of the reason for the failure at the time of invocation, such as assertNotNull ("object is empty", new Object()).
The above is how to use the test tool JUnit 4 shared by Xiaobian. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.