forked from opensearch-project/ml-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable tests with mockStatic in MLEngineTest (opensearch-project#2582)
* Enable test cases with mockStatic in MLEngineTest Signed-off-by: Liyun Xiu <[email protected]> * Create a class to provide mockStatic Signed-off-by: Liyun Xiu <[email protected]> * Add TODO and comment about static best practices Signed-off-by: Liyun Xiu <[email protected]> --------- Signed-off-by: Liyun Xiu <[email protected]>
- Loading branch information
Showing
2 changed files
with
43 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
ml-algorithms/src/test/java/org/opensearch/ml/engine/MLStaticMockBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.opensearch.ml.engine; | ||
|
||
import org.mockito.MockSettings; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.internal.creation.MockSettingsImpl; | ||
|
||
/** | ||
* This class provides a way to use Mockito's MockedStatic with inline mock maker enabled. | ||
* It can be used as a base class for other test classes that require mocking static methods. | ||
* | ||
* Note: before using this class to mock static function, think twice if your function really has to be | ||
* static as static functions are tightly coupled with coding using them and have bad testability. | ||
* | ||
* Example usage: | ||
* | ||
* public class MyClassTest extends MLStaticMockBase { | ||
* // Test methods go here | ||
* } | ||
* | ||
* It's to overcome the issue described in https://github.com/opensearch-project/OpenSearch/issues/14420 | ||
*/ | ||
public class MLStaticMockBase { | ||
private static final String inlineMockMaker = "org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker"; | ||
|
||
private MockSettings mockSettingsWithInlineMockMaker = new MockSettingsImpl().mockMaker(inlineMockMaker); | ||
|
||
protected <T> MockedStatic<T> mockStatic(Class<T> classToMock) { | ||
return org.mockito.Mockito.mockStatic(classToMock, mockSettingsWithInlineMockMaker); | ||
} | ||
|
||
protected <T> MockedStatic<T> mockStatic(Class<T> classToMock, MockSettings settings) { | ||
MockSettings newSettings = settings.mockMaker(inlineMockMaker); | ||
return org.mockito.Mockito.mockStatic(classToMock, newSettings); | ||
} | ||
} |