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 Springboot uses concrete classes and configurations to reduce unit test time

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how Springboot uses specific classes and configurations to shorten unit testing time, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Materialize classes and configurations to shorten unit test time

After writing the business code, we all need to write test cases to verify whether the code logic is correct, which is not only convenient for ourselves to check later, but also convenient for the people who take over to test our code quickly.

When writing a test case, we can use the following shortcut key: ctrl+shift+T, and then the following prompt appears:

Click, Create New Test....

Usually, it just helps us generate a corresponding class under the package, as well as the method name, and then we simply and rudely add to the class:

@ RunWith (SpringRunner.class) @ SpringBootTestpublic class SquirrelConfigTest {@ Test}

Note: the above comments load the classes and configuration files for the entire project, so that each time a test case is executed, if the project is large, it will take a long time. Therefore, we can specify the classes and configuration files to load as follows:

RunWith (SpringRunner.class) @ EnableAutoConfiguration@SpringBootTest (classes = {Test.class, Test2.class}) public class SquirrelConfigTest {}

It is obvious that the above time will be much shorter.

SpringBoot simple unit test 1. Why write unit test

A unit test is a small piece of code written by a developer to verify that a small, well-defined function of the target code is correct. Generally speaking, a unit test is used to determine the behavior of a particular function under a particular condition or scenario. For example, a user might put a large value into an ordered List and then confirm that the value appears at the end of the List. Alternatively, the user may delete characters from the string that match a certain pattern and then confirm that the string no longer contains those characters.

Unit testing is done by the programmer himself, and it is the programmer who will benefit in the end. It can be said that programmers have the responsibility to write functional code, as well as the responsibility to write unit tests for their own code. Unit tests are performed to prove that this code behaves as we expect.

In general, a functional module will often call other functional modules to complete a certain function, for example, the business class of the business layer may call multiple DAO to complete a business. When unit testing a functional module, we want to shield the external dependencies of the functional module in order to focus on the testing of the target functional module. At this time, the simulation object will be the most powerful tool, which simulates the specific operation behavior according to the interface of the external module, so that the unit test can verify the correctness of the logic of the module under the assumption that the associated module works correctly.

Second, the implementation of simple spring boot unit test.

1. First write a simple interface, as shown in the following figure:

Third, write unit tests

1. Open the test folder:

2. Specific code implementation

Notes for configuration:

3. If correct, as shown in the figure:

Error situation, as shown in the figure:

An error was reported because it was inconsistent with the expected return data:

Thank you for reading this article carefully. I hope the article "how Springboot uses specific classes and configurations to shorten unit testing time" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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