All about Java uses JUnit for unit testing

Source: Internet
Author: User

Unit Testing Unit Test

Unit testing is a short code program written by a programmer to test whether a specific method is running normally. As mentioned in head first Java, it is a good habit to write unit test code before writing code that implements actual functions, most of the time, after you finish writing a code that you think of as a function, will you write a test code specially for testing? It is estimated that this step will be skipped many times. Writing unit test code is not only used to complete functional code. After you modify the functional code, you can use the prepared unit test code to test the code immediately to ensure that the modified Code has no effect on the previous function.

Unit Testing with JUnit use JUnit for unit testing

JUnit 4.x uses the annotation method to mark which classes of methods have corresponding unit test code. JUnit assumes that all methods to be tested can be tested in any order. Therefore, the current test cannot depend on other test code. Test procedure using JUnit:

  • Mark the method to be tested with @ org. JUnit. Test.

  • The methods provided by JUnit are used to check the expected results and the actual running results.

You can use a tool like Eclipse or directly use the class "org. JUnit. Runner. junitcore" for testing.

Installation and Use of JUnit

From JUnit website
Download junit4.x. jar. The download contains the JUnit class library "junit-4. *. Jar ". Add this class library to your Java project and add it to classpath (). In eclipse, edit the build path to add it.

 

JUnit with Eclipse/eclipse using JUnit

Preparation

Create a new project "De. vogella. JUnit. First ". We will put the unit test code in a separate folder, so create a new source code folder "test", right-click the project, and click Properties, select "Java build path" in the left-side menu and click the source tab on the right.

Click "add folder", and then click "create new folder" in the daily window to create a "test" folder.

Creating a separate folder for the test code is not mandatory. However, we recommend that you store the test code separately from the normal code.

Create a Java class/create a Java class

Create the "de. vogella. JUnit. First" package and the following classes:

package de.vogella.junit.first;public class MyClass {public int multiply(int x, int y) {return x / y;}}

Create a JUnit test/create a JUnit Test

Select the class you created, right-click-> New-> JUnit test case, and change the source file folder to JUnit. Select New JUnit 4 test and make sure you change the original folder to test.

Click Next and select the method you want to test.

If you do not add JUnit to your classpath, eclipse will prompt you whether to add it to the classpath.

Create the following test code:

 

package de.vogella.junit.first;import org.junit.Test;import static org.junit.Assert.assertEquals;public class MyClassTest {@Testpublic void testMultiply() {MyClass tester = new MyClass();assertEquals("Result", 50, tester.multiply(10, 5));}}

Run your test via Eclipse/execute the test in eclipse

Right-click the test class you created and choose run-as-> JUnit Test

We should get a failed result after execution (as can be seen from the red progress bar ). Because our multiplier class does not run correctly now (the division number replaces the multiplication number in the class ). Modify the bug and run the test again. A green pass prompt is displayed.

If you have some tests, you can put them into test suite for merge execution. All test classes in test suite will be executed together when you run test suite. To create a new test suite, you can select multiple test classes to be added to the test suite, right-click it, and choose new-> Other-> JUnit-test suite.

Note: The method for creating test suite in this way will not find the test class for the test case based on junit4.x. (I am using myeclipse6.0, and other versions are not tested)

 

P.s. Refer to this article http://www-01.ibm.com/support/docview.wss? Uid = swg000063984 to solve this problem, we can create an empty test suite and manually edit the test case to be added to the test suite.

Change the code of test suite as follows to run your test case correctly. If you have developed some other test cases that need to be tested together, you can add them to @ suite. suiteclasses.

package mypackage;import org.junit.runner.RunWith;import org.junit.runners.Suite;@RunWith(Suite.class)@Suite.SuiteClasses( { MyClassTest.class })public class AllTests {}

Run the test with your own code

In addition to the above method, you can also write your own code for flexible testing, "org. JUnit. runner. the junitcore class provides the runclasses () method to run one or more test classes. This method returns a returned object of the "org. JUnit. Runner. Result" type. This object can be used to retrieve information related to test failure.

Create a new class "mytestrunner" in the "test" folder. The Code is as follows: (this class will execute your test class and output the test failure information on the console)

package de.vogella.junit.first;import org.junit.runner.JUnitCore;import org.junit.runner.Result;import org.junit.runner.notification.Failure;public class MyTestRunner {public static void main(String[] args) {Result result = JUnitCore.runClasses(MyClassTest.class);for (Failure failure : result.getFailures()) {System.out.println(failure.toString());}}}

For more information about marking and static references, see Baidu's document: http://wenku.baidu.com/view/7fffb323aaea998fcc220e21.html.

Address: http://www.vogella.de/articles/JUnit/article.html

The post-article information about marking and static reference methods is not translated. You can refer to the above Baidu document. The article has been edited a little and added some of my tested instructions.

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.