Skip to content

Instantly share code, notes, and snippets.

@Jahroots
Created July 19, 2016 13:27
Show Gist options
  • Save Jahroots/c06139287f5bbd8705d2af6d6533411f to your computer and use it in GitHub Desktop.
Save Jahroots/c06139287f5bbd8705d2af6d6533411f to your computer and use it in GitHub Desktop.
Mock hybris tenant
// -----------------------------------------------------------------------------------
// 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();
@ilyako87
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment