Last active
November 15, 2022 20:22
-
-
Save ivanov-alex/e0cc14d3dc6fc1520283 to your computer and use it in GitHub Desktop.
Extending Enum for UT using Mockito
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
/////////// changes for pom.xml | |
<dependency> | |
<groupId>org.powermock</groupId> | |
<artifactId>powermock-api-mockito</artifactId> | |
<version>1.6.2</version> | |
</dependency> | |
<dependency> | |
<groupId>org.powermock</groupId> | |
<artifactId>powermock-module-junit4</artifactId> | |
<version>1.6.2</version> | |
</dependency> | |
/////////// new imports | |
import java.lang.reflect.AccessibleObject; | |
import java.lang.reflect.Array; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Modifier; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.junit.runner.RunWith; | |
import org.mockito.internal.util.reflection.Whitebox; | |
import org.powermock.api.mockito.PowerMockito; | |
import org.powermock.core.classloader.annotations.PrepareForTest; | |
import org.powermock.modules.junit4.PowerMockRunner; | |
import antlr.CommonHiddenStreamToken; | |
import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
/////////// Annotation for class with tests | |
@RunWith(PowerMockRunner.class) | |
@PrepareForTest(ImportOrderOption.class) | |
/////////// test itself | |
@Test | |
public void testVisitTokenSwitchReflection() throws Exception { | |
ImportOrderOption C = PowerMockito.mock(ImportOrderOption.class); | |
Whitebox.setInternalState(C, "name", "NEW_OPTION_FOR_UT"); | |
Whitebox.setInternalState(C, "ordinal", 5); | |
PowerMockito.mockStatic(ImportOrderOption.class); | |
PowerMockito.when(ImportOrderOption.values()).thenReturn(new ImportOrderOption[]{ | |
ImportOrderOption.TOP, ImportOrderOption.ABOVE, ImportOrderOption.INFLOW, | |
ImportOrderOption.UNDER, ImportOrderOption.BOTTOM, C}); | |
PowerMockito.mockStatic(Enum.class); | |
PowerMockito.when(Enum.valueOf(ImportOrderOption.class,"TOP")).thenReturn(ImportOrderOption.TOP); | |
PowerMockito.when(Enum.valueOf(ImportOrderOption.class,"ABOVE")).thenReturn(ImportOrderOption.ABOVE); | |
PowerMockito.when(Enum.valueOf(ImportOrderOption.class,"INFLOW")).thenReturn(ImportOrderOption.INFLOW); | |
PowerMockito.when(Enum.valueOf(ImportOrderOption.class,"UNDER")).thenReturn(ImportOrderOption.UNDER); | |
PowerMockito.when(Enum.valueOf(ImportOrderOption.class,"BOTTOM")).thenReturn(ImportOrderOption.BOTTOM); | |
PowerMockito.when(Enum.valueOf(ImportOrderOption.class,"NEW_OPTION_FOR_UT")).thenReturn(C); | |
DetailAST astImport = mockAST(TokenTypes.IMPORT, "import", "mockfile", 0, 0); | |
DetailAST astIdent = mockAST(TokenTypes.IDENT, "myTestImport", "mockfile", 0, 0); | |
astImport.addChild(astIdent); | |
DetailAST astSemi = mockAST(TokenTypes.SEMI, ";", "mockfile", 0, 0); | |
astIdent.addNextSibling(astSemi); | |
ImportOrderCheck check = new ImportOrderCheck (); | |
check.setOption("NEW_OPTION_FOR_UT"); | |
// expecting IllegalStateException | |
check.visitToken(astImport); | |
} | |
/////////// mocking method from test | |
/** | |
* Creates MOCK lexical token and returns AST node for this token | |
* @param tokenType type of token | |
* @param tokenText text of token | |
* @param tokenFileName file name of token | |
* @param tokenRow token position in a file (row) | |
* @param tokenColumn token position in a file (column) | |
* @return AST node for the token | |
*/ | |
private static DetailAST mockAST(final int tokenType, final String tokenText, | |
final String tokenFileName, final int tokenRow, final int tokenColumn) | |
{ | |
CommonHiddenStreamToken tokenImportSemi = new CommonHiddenStreamToken(); | |
tokenImportSemi.setType(tokenType); | |
tokenImportSemi.setText(tokenText); | |
tokenImportSemi.setLine(tokenRow); | |
tokenImportSemi.setColumn(tokenColumn); | |
tokenImportSemi.setFilename(tokenFileName); | |
DetailAST astSemi = new DetailAST(); | |
astSemi.initialize(tokenImportSemi); | |
return astSemi; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment