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 JUnit5 and indicator Monitoring of SpringBoot2

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

Share

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

This article mainly introduces "how to use SpringBoot2 JUnit5 and indicator monitoring". In daily operation, I believe many people have doubts about how to use SpringBoot2 JUnit5 and indicator monitoring. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use SpringBoot2 JUnit5 and indicator monitoring". Next, please follow the editor to study!

1 introduction to unit test JUnit 51.1JUnit 5 and its use

   JUnit5 as the latest version of the JUnit framework is very different from the previous version of the Junit framework. JUnit5 is mainly composed of several different modules of three different subprojects: JUnit Platform, JUnit Jupiter, and JUnit Vintage.

JUnit Platform: it is the basis for launching the testing framework on JVM. Not only does it support Junit's homemade test engine, but other test engines can also be accessed.

JUnit Jupiter: provides a new programming model for JUnit5 and is the core of JUnit5's new features. A test engine is included internally to run on Junit Platform.

JUnit Vintage: since JUint has been developed for many years, JUnit Vintage provides a JUnit4.x,Junit3.x-compatible test engine to take care of older projects.

What we need to know about    here is that version 2.4 of SpringBoot removes the default dependency on JUnit Vintage. If you need to be compatible with junit4, you need to introduce it on your own. If you don't, you can't use the junit4 function @ Test, but JUnit5 also has a @ Test annotation that can accomplish the same function.

Use: from the above JUnit5 analysis, if you want to use JUnit5 and be compatible with previous versions of JUnit, you must refer to the following two dependencies

Org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine test org.hamcrest hamcrest-core

Then annotate the test class with @ SpringBootTest annotation and the test method with @ Test

1.2 commonly used test notes

All the notes have been defined and explained in the official    documentation. You can view the link address of the official document through this link. Next, you will select a few commonly used notes to explain and learn.

@ SpringBootTest and @ Test will not repeat

@ DisplayName note: console brick display name

@ BeforeEach comment: execute @ AfterEach note before each method execution: execute after each method execution

@ BeforeAll annotations: execute @ AfterAll annotations before all methods are executed: it is worth noting that the modified methods of both annotations must be modified with static

@ Disable note: disable this method when running the test class. You can execute the @ Timeout annotation alone: set the execution time of the test method. If the time exceeds this time, an exception is thrown.

@ ExtendWith annotation: set the test driver of the test class. @ SpringBootTest annotation is a composite annotation, which is found at the bottom

The @ ExtendWith annotation specifies that the test class uses SpringBoot's test driver

@ RepeatedTest comment: specify the number of times the method is repeated

1.3 assertions (assertions)

   assertion is the core part of the test method, which is used to verify the conditions that the test needs to meet. The so-called assertion is to check whether the data returned by the business logic is reasonable, and a detailed test report will be generated after all the test runs are completed. It is worth noting that when an assertion fails, all code that follows from that assertion will not execute the JUnit 5 built-in assertions, which can be divided into the following categories:

Simple assertion

The above methods are used as follows (take the assertEquals method as an example):

Array assertion

Combination assertion combination assertion will not be passed until all parameters are satisfied, otherwise an error assertion will be returned, and multiple simple assertions can be made in one method.

Exception assertion fails when the result does not throw a defined exception

Timeout assertion returns an assertion when the defined timeout time is exceeded

Quick assertions return assertions when logical

The advantage of    's use of assertions is that you can run all the methods of the test class using the methods in the following picture, and run normally if all the assertions pass. Otherwise, all assertion failures will be returned.

1.4 preconditions (assumptions)

The preconditions (also known as hypotheses) in    JUnit 5 are similar to assertions, except that unsatisfied assertions cause the test method to fail, while unsatisfied preconditions only terminate the execution of the test method. The precondition can be regarded as the premise of the test method execution, when the premise is not satisfied, there is no need to continue the execution.

As can be seen from the above figure, the biggest difference between the precondition and the assertion is that if the precondition is not satisfied, skip the method and continue to execute other methods, but if the assertion is not satisfied, the following code will not be executed.

1.5 nested testing

   nested testing means that there are test classes in the test class. In this case, the outer test cannot drive the inner Before (After) All (Each) method, but the inner test can drive the outer Before (After) All (Each) method.

1.6 Parametric testing

@ ValueSource: specify the source of input parameters for parameterized testing, and support eight basic classes as well as String type and Class type

@ NullSource: provide the parameter null for the test method

EnumSource: provides enumerated parameters for test methods

@ CsvFileSource: reads the contents of the specified CSV file as a parameterized test input parameter

MethodSource: reads the return value of the specified method as a parameterized test input parameter (but the return of this method must be a Stream stream)

2 Index Monitoring 2.1 using url to realize Monitoring

Reference Actuator initiator

Org.springframework.boot spring-boot-starter-actuator

Open the desired endpoint using the configuration file

Management:

Endpoints:

Enabled-by-default: false # enables all monitoring endpoints by default. If customized, it needs to be disabled.

Web:

Exposure:

Include:'*'# expose all endpoints in the form of web

Endpoint:

Beans:

Enabled: true

Health:

Enabled: true

Show-details: always

Info:

Enabled: true

Metrics:

Enabled: true

Use url to view monitoring information

2.2 Visual monitoring platform

Use Spring Initializr to quickly create a SpringBoot project, add the pom file of the project to the dependency of the service monitoring platform, and add @ EnableAdminServer annotation to the main program class of the project

De.codecentric spring-boot-admin-starter-server 2.3.1

In order to prevent port conflicts with services to be monitored, you need to modify the port number of the monitoring in the configuration file.

Server:

Port: 8888

Register other services for monitoring

Step 1: import dependencies into the monitored service

De.codecentric spring-boot-admin-starter-client 2.3.1

Step 2: the configuration file in the monitored service configures the url of the monitoring project

Spring:

Boot:

Admin:

Client:

Url: http://localhost:8888

Instance:

Prefer-ip: true

Application:

Name: Project-admin_manage

At this point, the study on "how to use SpringBoot2 JUnit5 and indicator monitoring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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