Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the ea-async error handling of Junit Test Code

Shulou Source: shulou.com Published: 2022-06-03 07:14:43 11月15日 Update

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!

Tags: Code error test processing method problem learning grammar stage information writing function way scheme more essence module trouble easy to use help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Shulou Information Redmi MariaDB