JUnit Test Notes (i)

Source: Internet
Author: User

In the development of projects, it is unavoidable to use unit testing.      Let's start with some basic concepts. 1) @Test @ is the meaning of the annotation, @Test is the basis of the JUnit test, which means that the method under the note is run with JUnit testing. 2) Import package problem to use JUnit unit tests to import two packages first: 1) Import the Junit.jar package (Junit4-4.8.2.jar), and then import org.junit.Test;2) ImportHamcrest-core-1.3.jarThese two packages must be available for junit testing, which are available on all two jar resources, and the CSDN download channel3) Error problem1) @Test The red line indicates that you did not import Junit.jar2) When error java.lang.noclassdeffounderror:org/hamcrest/selfdescribing indicates that you did not import Hamcrest-core-1.3.jar is the approximate framework of the project 。 SRC puts the project source file. TESTSRC put the test class. Referenced libiraires into the Lib folder.Junit4-4.8.2.jar and Hamcrest-core-1.3.jar.
the next step is to explain the specific operation1) First write the core class and the main method, the class writing SRC package, the code attached to the last side of the code area;2) Then write the test class, generally re-build a TESTSRC package (with the SRC package sibling), and then build the test class under the TESTSRC;3) Run the test method in the test class to view the output results. in the test class, you want to import the test class:import org.jnuit.Test;The @test annotation must be added to the method of testing the main method at the same time, otherwise the unit test cannot be done1) First write the core class and the Main method, this kind of writing SRC package, the code attached to the last side of the article Code area SRC code in different projects also different, do not say. In the example attached below is just an output statement 2) and then write the test class, generally re-build a TESTSRC package (with the SRC package sibling), and then build the test class under the TESTSRC main process steps for building test classesTo set up a test class on the package chart right-click (in my project is Testsrc.test)-tap New-->other, then enter case, when you see the JUnit test, select the package name-- For class under test--> in the pop-up window input the classes to be tested, here is the main package inside the unitmain.java--> Click OK, select Next--> in the pop-up window select the method to detect UM () Finally click Finish---> Write the logic of test class Utest Enter caseWhen you see the JUnit Test case, select

for the class under test column, select Browse

in the popup window, enter the class to be tested, and here is the Unitmain.java inside the main package.

in the pop-up window, select the method you want to detect UM ()

Write the logic of the test class utest, the specific code put in the end of the article

3) Run the test method in the test class to view the output results.
Click to Select utest.java--> Right click to select the method testum ()---> Check run as--> select JUnit Test
< Span style= "font-family: Microsoft Jacob Black; font-size:14px; line-height:21px ">
< Span style= "font-family: Microsoft Jacob Black; font-size:14px; line-height:21px "> when running without errors, you can see that the chart for JUnit test is a green checkmark, otherwise a red Cross

Output results

Source code:
source code for Unitmain.java in src
Package Main;public class Unitmain {public void um () {//Main method System.out.println ("Unit Test") to be tested;}}
Source code for Utest.java in TESTSRC
Package Test;import Main.unitmain;import Org.junit.test;public class Utest {   unitmain um=new unitmain ();    @Test public    void Testum ()   {        um.um ();        SYSTEM.OUT.PRINTLN ("Test um");}    }




JUnit Test Notes (i)

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.