Introduction to JUnit and debug mode for Java

Source: Internet
Author: User

Introduction to JUnit and debug mode for Java

Getting Started with 1.Junit

Import the appropriate version of Eclipse's built-in Junit-junit-junit, in the following manner 1:

Mode two uses MAVEN to load as follows (see following maven):

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.8</version>

<scope>test</scope>

</dependency>

</dependencies>

If you need to find a dependency please go to the official web search, such as the following

https://junit.org/junit5/docs/current/user-guide/

The common methods of 2.JUNIT4

@Test test methods

@Ignorde ignored test methods

@Before run before each method test

@After run after each method

@BeforeClass run before all test methods, static expressions are required

@AfterClass run after all test methods, need to be static expression

public class Junittest {

@After

public void init () {

System.out.println ("Init");

}

@Before

public void Destroy () {

System.out.println ("after");

}

@Test

public void Test () {

SYSTEM.OUT.PRINTLN ("test");

}

@BeforeClass

public static void Testbeforeclass () {

System.out.println ("Beforeclass");

}

@AfterClass

public static void Testafaterclass () {

System.out.println ("Afterclass");

}

}//beforeclass before test after Afterclass

So the order is beforeclass before test after Afterclass

Common methods for JUNIT5 (slightly different than junit4 expressions)

The @BeforeAll executes only once and runs before all test methods (similar to Beforeclass) and requires a static method.

@BeforeAll execute only once, before all test methods run (similar to afterclass), require a static method

@BeforeEach run before each test method, similar to (@Before)

@AfterEach run after each test method, similar to (@After)

@Test

@DisplayName change the name that is displayed in the test interface.

@Disabled ignored (similar to @ignore)

public class Junittest {

@BeforeAll

public static void Initbefore () {

System.out.println ("Beforeall");

}

@AfterAll

public static void Destroyafter () {

System.out.println ("Destroyafter");

}

@Test

@DisplayName ("Testhello")

public void Test () {

SYSTEM.OUT.PRINTLN ("test");

}

@BeforeEach

public void Testbeforeeach () {

System.out.println ("Beforeeach");

}

@AfterEach

public void Testaftereach () {

System.out.println ("Aftereach");

}

@Test

@Disabled

@DisplayName ("Testhello")

public void Test1 () {

SYSTEM.OUT.PRINTLN ("test");

}

}

The output results are sequentially

Beforeall Beforeeach Test Aftereach Destroyafter

Failures Test Run Error

The runtime error of the Errors program itself

Assertion of 3.Junit

public class Junitassert {

@Test

public void Standardassertions () {

Assertequals (2, 2);

Assertequals (4, 4, "The optional assertion message is today the last parameter."); Asserttrue (' A ' < ' B ', ()--"assertion messages can be lazily evaluated--"

+ "To avoid constructing complex messages unnecessarily.");

}

@Test

public void Groupedassertions () {

Assertall ("Person",

() Assertequals ("John", "John"),

() assertequals ("Doe", "Doe")

);

}

@SuppressWarnings ("Static-access")

@Test

public void timeoutnotexceeded () throws Interruptedexception {

Asserttimeout (Duration.ofseconds (2), ()->{new Thread (). Sleep (1000);});

}

@Test

public void Assertwithhamcrestmatcher () {

Assertthat (2 + 1, is (Equalto (3)));

Assertthat (2 + 1, not (3));

/* There are also containsstring EndsWith Startwith

Equlto equaltoignoringcase allof (bigger and smaller than a certain size)

AnyOf (satisfies a condition) Greatthan (greater than) LessThan (less than)

Closeto Positive and negative Haskey (map collection) HasValue (map collection), etc.

These methods lead to some trouble, you need to copy the import static org.hamcrest.CoreMatchers.not to change the not to the appropriate method

*/

}

}

There are many other methods that can be https://junit.org/junit5/docs/current/user-guide/to view

4.Debug mode

Breakpoint Debug Test related buttons, or the right mouse button debug as to indicate that the step is to enter the method inside the method, point to the next line is the method does not enter the method inside, point to the next line, run to the next breakpoint from the method inside the next line of jumping out of the method is not running the following line of code inside, Returns to the starting position of the current method row.

Run the related variable parameter table

Run related information

Introduction to JUnit and debug mode for Java

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.