Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JUnit

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use JUnit". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use JUnit.

Junit framework:

Junit is an open source java unit testing framework.

Today we introduce that junit5,junit5 consists of three different subproject modules, including Junit Platform, Junit Jupiter, and Junit Vintage. It supports Java8 and above. I use IntelliJ IDEA here in the editor. I only need to look at the final result to know whether the method interface of the whole project is smooth. Each unit test case is relatively independent, started by Junit and called automatically. There is no need to add additional call statements.

Add, delete, and shield test methods without affecting other test methods.

Junit is mainly used for white-box testing. The steps for white-box testing are as follows:

1. Test plan phase: make the test progress according to the requirement specification.

2. Test design phase: according to the function of the code, test cases are designed manually to test the basic functions. According to the program design specification, the software structure is divided and the test cases are designed according to a certain standardized method.

3. Test execution phase: input test cases and get test results.

4. test summary stage: compare the test results with the expected results of the code, analyze the causes of the errors, find and solve the errors.

Next, let's introduce the notes inside:

Test: indicates the test method without declaring any properties.

ParameterizedTest: indicates that the method is a test method, and this comment also allows a test method to be run multiple times with different people.

RepeatedTest: this annotation allows the test method to customize the number of repeat runs.

TestFactory: indicates that a method is based on a data-driven dynamic test data source.

Displayname: declares a custom display name for a test class or test method.

BeforeEach: indicates that the specified method is run before each test method is run.

AfterEach: indicates that the specified method is run after each test method is run.

BeforeAll: executes before all test methods of the current class, which can contain some initialization code.

AfterAll: executes after all test methods of the current class.

Disabled: indicates that a test class or method is invalid.

Assert:

Fail: asserts that the test failed.

AssertTrue/asserFalse: assert whether it is true or false.

AssertNull/assertNotNull: the assertion is empty or not empty.

AssertEquals/assertNotEquals: asserts that two values are not equal.

AssertArrayEquals: asserts that the array elements are all the same.

AssertSame/assertNotSame: asserts whether two objects are the same.

AssertThrows/assertDoesNotThrow: asserts whether an exception is thrown.

AssertAll: asserts that multiple conditions are met

Let's create a class and create several methods:

Package test;// package mechanism: import java.lang.reflect.Array;public class MixedOperation {public int mixeOperation (int xpenint y) {return division ((xcamy), y);} public int division (int xreint y) {int result= (xcompy); return result;} public static void main (String [] args) {MixedOperation mixedOperation=new MixedOperation (); System.out.println (mixedOperation.mixeOperation (5jin1));}}

We created a class called MixedOperation

Package test;import org.junit.jupiter.api.*;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.CsvSource;public class MixedOperationTest {private MixedOperation mixedOperation; @ BeforeEach public void init () {mixedOperation=new MixedOperation ();} @ Test public void successTest () {System.out.println ("run a test:x=4,y=2"); int result=mixedOperation.mixeOperation (4); Assertions.assertEquals (3) } / * @ DisplayName ("failure") public void errorTest () {`System.out.println ("run a test:x=4,y=0"); ArithmeticException exception=new ArithmeticException (ArithmeticException.class-> {mixedOperation.mixeOperation (4,0);}) } * / @ Disabled ("parameters") @ Test @ DisplayName ("parameters") @ ParameterizedTest @ CsvSource ({"6pje 3pr 3", "5pr 2jue 3", "6pr 2pint 4"}) public void caTest (int x recorder int yint excepted) {System.out.println ("run a test: X =" + x + "y =" + y); System.out.println (excepted); int t = mixedOperation.mixeOperation (xQuery y) Assertions.assertEquals (excepted,t);} @ Disabled @ Test public void Next () {System.out.println ("throw an exception"); System.out.println (Assertions.assertThrows (IllegalArgumentException.class, ()-> mixedOperation.mixeOperation (2);} @ Disabled @ Test void error () {Assertions.assertThrows (Exception.class, ()-> {Assertions.assertEquals (1);}) } @ Test void sure () {int result=mixedOperation.mixeOperation (4jin2); Assertions.assertTrue (3==result); / / assert equality}}

This is where we create a test class called MixedOperationTest

At this point, I believe you have a deeper understanding of "how to use JUnit". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report