Created
July 19, 2016 13:27
-
-
Save Jahroots/c06139287f5bbd8705d2af6d6533411f to your computer and use it in GitHub Desktop.
Mock hybris tenant
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 unit test does not add value to the code quality, but to show off how to mock | |
// PRIVATE and STATIC fields of a class as well as how to mock TENANT in Registry | |
// ----------------------------------------------------------------------------------- | |
final AbstractTenant mockTenant = Mockito.mock(AbstractTenant.class); | |
final Field fieldStopping = AbstractTenant.class.getDeclaredField("currentlyStopping"); | |
fieldStopping.setAccessible(true); | |
ReflectionUtils.setField(fieldStopping, mockTenant, Boolean.TRUE); | |
final Field field = Registry.class.getDeclaredField("_currentTenant"); | |
field.setAccessible(true); | |
final ThreadLocal<Object> currentTenant = (ThreadLocal<Object>) field.get(null); | |
currentTenant.set(mockTenant); | |
final ConfigIntf config = Mockito.mock(ConfigIntf.class); | |
Mockito.doReturn(config).when(mockTenant).getConfig(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for this example!
But it would be just as cool to redefine applicationContext.
For example i have some error:
Exception in thread "Thread-0" Exception in thread "Thread-1" java.lang.IllegalStateException: Can not activate tenant Mock for AbstractTenant, hashCode: 340715687 since it has no application context created