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 error report of Mockito mock Kotlin Object class method

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

Share

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

Today, I will talk to you about how to solve the problem of error reporting in Mockito mock Kotlin Object methods. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

For example, I create a Kotlin Object class: ObjectMethod

Package com.baichuan.example.unit_testobject ObjectMethod {fun doSomething () {println ("this is ObjectMethod#doSomething")} @ JvmStatic fun doSomethingWithJvmStatic () {println ("this is ObjectMethod#doSomethingWithJvmStatic")}}

If I go directly to the doSomething method of the mock class, I will report an error.

@ Test @ DisplayName ("mock ordinary kotlin static method") fun testMockKotlinObject () {Assertions.assertThrows (MissingMethodInvocationException::class.java) {Mockito.mockStatic (ObjectMethod::class.java) .`room` (ObjectMethod::doSomething). ThenAnswer {println ("this is mocked Object#doSomething")}} ObjectMethod.doSomething ()}

This is because the methods in the object class in kotlin are no different from static methods in terms of form and use in kotlin. But when compiled into java code, the essence is that a static constant instance of the current class INSTANCE is initialized internally. This INSTANCE is hidden in the kotlin syntax, but access can still be displayed in java. The code after ObjectMethod is compiled into java is as follows:

Public final class ObjectMethod {@ NotNull public static final ObjectMethod INSTANCE = new ObjectMethod (); private ObjectMethod () {} public final void doSomething () {String var1 = "this is ObjectMethod#doSomething"; boolean var2 = false; System.out.println (var1);} @ JvmStatic public static final void doSomethingWithJvmStatic () {String var0 = "this is ObjectMethod#doSomethingWithJvmStatic"; boolean var1 = false; System.out.println (var0);}}

Therefore, the essential reason for not being able to mock ObjectMethod#doSomething is that the normal means cannot mock static constants. If you want a method in kotlin's object class to be mock, simply annotate the method with @ JvmStatic. The annotated method is compiled into a normal java static method.

It is said above that normal methods cannot mock static constants, so what about abnormal methods? In fact, this abnormal method is to inject the mock instance into the ObjectMethod through reflection.

@ Test @ DisplayName ("mock ordinary kotlin static methods by modifying static constants through reflection") fun testMockKotlinObjectMethodByReflection () {val mock = Mockito.mock (ObjectMethod::class.java) Mockito.`room` (mock.doSomething ()) .then {print ("this is mocked ObjectMethod by reflection")} val declaredMethod = ObjectMethod::class .java.getDeclaredField ("INSTANCE") ReflectionUtils.setFinalStatic (declaredMethod Mock) ObjectMethod.doSomething ()}

ReflectionUtils

Package com.baichuan.example.unit_testimport java.lang.reflect.Fieldimport java.lang.reflect.Modifierobject ReflectionUtils {@ Throws (Exception::class) fun setFinalStatic (field: Field, newValue: Any) {field.isAccessible = trueval modifiersField: Field = Field::class.java.getDeclaredField ("modifiers") modifiersField.isAccessible = true modifiersField.setInt (field, field.modifiers and Modifier.FINAL.inv () field.set (null, newValue)}} read the above content Do you have any further understanding of how to solve the problem of reporting errors in Mockito mock Kotlin Object-like methods? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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