-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: current context for test classes (#180)
- Loading branch information
Showing
4 changed files
with
69 additions
and
57 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextConfig.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,12 @@ | ||
package ch.sbb.polarion.extension.generic.context; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CurrentContextConfig { | ||
String value() default "text-extension"; | ||
} |
35 changes: 35 additions & 0 deletions
35
app/src/test/java/ch/sbb/polarion/extension/generic/context/CurrentContextExtension.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 ch.sbb.polarion.extension.generic.context; | ||
|
||
import ch.sbb.polarion.extension.generic.rest.model.Context; | ||
import ch.sbb.polarion.extension.generic.util.ContextUtils; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
public class CurrentContextExtension implements BeforeEachCallback, AfterEachCallback { | ||
|
||
private MockedStatic<ContextUtils> contextUtilsMockedStatic; | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext extensionContext) throws Exception { | ||
contextUtilsMockedStatic = Mockito.mockStatic(ContextUtils.class); | ||
|
||
String contextName = "test-extension"; | ||
CurrentContextConfig currentContextConfig = extensionContext.getRequiredTestClass().getAnnotation(CurrentContextConfig.class); | ||
if (currentContextConfig != null) { | ||
contextName = currentContextConfig.value(); | ||
} | ||
|
||
Context context = new Context(contextName); | ||
contextUtilsMockedStatic.when(ContextUtils::getContext).thenReturn(context); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext extensionContext) throws Exception { | ||
if (contextUtilsMockedStatic != null) { | ||
contextUtilsMockedStatic.close(); | ||
} | ||
} | ||
} |
24 changes: 2 additions & 22 deletions
24
app/src/test/java/ch/sbb/polarion/extension/generic/properties/GetterFinderTest.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
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