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 for unit testing

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use Junit for unit testing". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn "how to use Junit for unit testing".

Junit introduction

Junit is not a package that comes with java, so you should introduce a third-party jar package when you use it.

You can also use maven for project management and maven for the introduction of Junit packages (the following code is a dependent reference to a version of Junit):

Junit junit 4.11 test

Finally, some IDE will import the Junit package themselves when creating the project (or you can set up to import the Junit package, such as eclipse). When creating the project, these IDE will divide the src folder into two folders, one for main and the other for test, so you can write Junit test code directly in the test folder.

How to write unit test code

The unit test code can be simply divided into three steps, namely Given,When,Then. Given represents the initial state or precondition (simply understood as data input); When represents the occurrence of the behavior, that is, the test action; and Then is the assertion result.

So in most cases, we can divide the unit test code into three pieces:

The first piece of code is responsible for data processing before testing, such as input data, etc.

The second block of code is responsible for calling the relevant functional modules for testing.

The third block of code is responsible for comparing the test results with the results we expect to get the test results (pass or fail).

Code actual combat

Taking a simple calculator class as an example, here are the methods for this calculator class:

Public class Calculator {public static int add (int x pencil int y) {return x int y;} public static int substract (int x cent int y) {return x m int y;} public static float divide (int x cent int y) {return x int y;}}

We designed some simple test code (for more information on the code interpretation, see the notes):

Import org.junit.Assert

Import org.junit.Test

Public class CalculatorTest {

@ Test / / use the test annotation, and the method under the annotation is the test code

Public void testAdd () {

/ / what we are going to test is an add function that passes in two int parameters

Int Xerox 2 recording YBG 3 paddle / input data

Int res = Calculator.add (xQuery y); / / pass the data to the function under test, and then store the returned result in res

Assert.assertEquals (res, 5); / / use assertions to determine whether the value of res is equal to the expected value (here expectation is 5)

/ / if the value of res is equal to the expected value, the test passes, otherwise the test fails

}

@ Test

Public void testSubstract () {

/ / this is another test code, using abbreviations to put the first and second steps together.

Int res = Calculator.substract (5pm 3)

Assert.assertEquals (res, 3)

}

} at this point, I believe you have a deeper understanding of "how to use Junit for unit testing". 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