The application of Junit unit test in IntelliJ idea __junit

Source: Internet
Author: User
Tags generator

IntelliJ idea is most people generally recognized the best use of the IDE, but myeclipse after all the Overlord has done so many years, the Internet can find a comparative system of basic tutorials are myeclipse, so here to write an article IntelliJ Idea for a detailed graphic tutorial on unit testing.

IntelliJ idea has integrated the Junit package internally, so you don't need to download it yourself. 1 Why use JUnit

This is not too obscure and difficult to understand, only from the perspective of our use of feelings.

As we all know, the main method is the entrance to a program, and normally, without the main method, the program cannot run. We often write some class files (as shown below), and they don't have their own main method. So how do we detect if these class writes are correct. Do you want to write a main method for each class each time you test it? This obviously costs too much. The JUnit unit test provides us with the convenience of testing a class without the main method directly.

2 The use of JUnit in IntelliJ idea

Every time you write your own JUnit test code is too cumbersome, IntelliJ idea provides a very handy plug-in: JUnit generator, which automatically generates JUnit test code. The specific installation method is:

Open Settings (ctrl+alt+s) to find plugins

Enter Junit generator V2.0 in the Plugins search box and click Install

Back to the class file we wrote earlier, Alt+insert, choose JUnit 4

A test class package is automatically generated in the SRC file (test in the following figure). Unitexample), and automatically generate test class files (Persontest in the following illustration)

is likely to not have been imported into the work path for @test in test class, which is not compiled. JUNIT4 Does not matter, has been alt+enter, according to the prompt solution on the line, really is the artifact. Write the appropriate code in each method you want to test. Finally, test class can compile the

Package test. 

Unitexample;
Import Unitexample.person;
Import Org.junit.Test; 
Import Org.junit.Before; 

Import Org.junit.After; 
/** * Person Tester. * * @author <authors name> * @since <pre> 2016</pre> * @version 1.0/public class Persont 

EST {@Before public void before () throws Exception {System.out.println ("before"); 

@After public void After () throws Exception {System.out.println (' after ');}
    /** * * * method:watch () */@Test public void Testwatch () throws Exception {person person = new person ();
Person.watch ();  }/** * * Method:setname (String name) */@Test public void Testsetname () throws Exception {person person =
    New Person ();
Person.setname ("Emma");
        }/** * * method:getname () */@Test public void Testgetname () throws Exception {
        person who = new person ();
        Person.setname ("Emma");
    System.out.println (Person.getname ()); 
 }

}

The

5 ctrl+shift+f10 runs with green strips and expected results, indicating that the unit test was successful and that the original class met the requirements. In addition, in the lower left of the figure below, you can also choose to test the class inside a specific test method, and then the same ctrl+shift+f10 run, the same as the previous

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.