掲示板
Mocking @Reference objects
6年前 に Christopher Virtucio によって更新されました。
Mocking @Reference objects
Junior Member 投稿: 33 参加年月日: 17/09/26 最新の投稿
I'm writing a unit test for a component that has @Reference field members. How do I mock these @Reference objects?
6年前 に Danielle Ardon によって更新されました。
RE: Mocking @Reference objects (回答)
Junior Member 投稿: 37 参加年月日: 16/06/06 最新の投稿
Hi Christopher,
you can just mock them like you would normally, for example for this class:
We use this:
Hope that helps!
you can just mock them like you would normally, for example for this class:
@Component(immediate = true, service = Category.class)
public class CategoryImpl implements Category {
@Reference
private CompanyLocalService companyService;
@Reference
private AssetCategoryLocalService assetCategoryService;
....more code with methods etc.
}
We use this:
import static org.mockito.MockitoAnnotations.initMocks;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.liferay.asset.kernel.service.AssetCategoryLocalService;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.kernel.util.PropsUtil;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ PropsUtil.class, CategoryImpl.class})
public class CategoryImplTest {
@Mock
private CompanyLocalService companyService;
@Mock
private AssetCategoryLocalService assetCategoryLocalService;
@InjectMocks
private CategoryImpl categoryImpl;
@Before
public void setUp() throws PortalException {
categoryImpl = new CategoryImpl();
PowerMockito.mockStatic(PropsUtil.class);
initMocks(this);
}
@Test
Hope that helps!