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 solve the ea-async error handling of Junit Test Code

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to solve the ea-async error handling of Junit test code". In the daily operation, I believe many people have doubts about how to solve the problem of ea-async error handling of Junit test code. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to solve the ea-async error handling of Junit test code". Next, please follow the editor to study!

Problem background

The ea-async syntax sugar await is used in the project code, and an error occurs when writing the Junit test code.

Error message Error during attachment using: com.ea.async.shaded.net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@6e4f263e

Cause of the problem

The error message shows that the initialization failed. The essence of the initialization failure is that the package of ea-async cannot be found. But ea-async clearly exists, why can't it be found? This is because what the sugar nature of ea-async syntax does is compile weaving, which implements the processing of CompletableFuture objects during the bytecode generation phase. The technology used here should be an abstract syntax tree. Interested students can continue to explore. Going back to this question, how do you trigger code weaving for ea-async? It's simple to add the ea-async-maven-plugin plug-in during the module packaging phase, and then perform maven packaging.

Solution

Introducing ea-async-maven-plugin into pom using await module

Com.ea.async ea-async-maven-plugin 1.2.3 Instrument instrument-test

Perform maven packaging

Mvn clean package-Dmaven.test.skip=true

Personal preference is mvn clean install-Dmaven.test.skip=true (install overrides the package phase)

Other circumvention schemes

Main function way if you think that each mvn package is too troublesome, and the test code is not going to submit, just test the function, you can also directly write a Main function call. It is too troublesome to modify JVM mode, which has not been studied.

Considerations for the use of await

When a method using await has a return value, the returned method type must be wrapped in CompletionStage or a subclass (such as CompletableFuture). The following example shows how to make a correct month error:

The called code public CompletableFuture calledMethod () {return CompletableFuture.completedFuture (1);} is incorrectly written public boolean errorCallAwait () {int num = await (calledMethod ()); return num = = 1;} correct public CompletableFuture rightCallAwait () {int num = await (calledMethod ()); return CompletableFuture.completedFuture (num = = 1) At this point, the study on "how to solve the ea-async error handling of Junit test code" 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