下面列出了怎么用org.mockito.internal.matchers.Any的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void doFilter_givenUrlMidIsValid() throws Exception {
final String mId = "fdfb4933";
fakeRequest(mId, null);
when(mpSvcMock.getMarketplaceById(mId)).thenReturn(new VOMarketplace());
mpCtxFilter.doFilter(requestMock, responseMock, chainMock);
verify(sessionMock, times(1)).setAttribute(
Matchers.eq(Constants.REQ_PARAM_MARKETPLACE_ID),
Matchers.eq(mId));
// no MPL portal request, thus no redirect required
verify(responseMock, never()).sendRedirect(
Matchers.contains(Marketplace.PUBLIC_CATALOG_SITE));
// if mId given, do not use fallback via subscription key or cookie
verify(mpSvcMock, never()).getMarketplaceForSubscription(
Matchers.anyLong(), Matchers.anyString());
verify(requestMock, times(1)).getCookies();
verify(sessionMock, never()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, Any.class);
assertEquals(mId, sessionMid);
}
@Test
public void doFilter_noValidMidInGivenUrl_mIdStoredInCookie()
throws Exception {
final String cookieId = fakeSetCookieMid(
Constants.REQ_PARAM_MARKETPLACE_ID, "myMpId");
fakeUserLogin("myOrgId");
fakeCreateMarketplace(cookieId);
mpCtxFilter.doFilter(requestMock, responseMock, chainMock);
verify(sessionMock, atLeastOnce()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, cookieId);
verify(sessionMock, never()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, Any.class);
assertEquals(cookieId, sessionMid);
}
@Test
public void doFilter_MpRedirect() throws Exception {
when(requestMock.getServletPath()).thenReturn(
BaseBean.MARKETPLACE_REDIRECT);
mpCtxFilter.doFilter(requestMock, responseMock, chainMock);
// MPL portal request, thus no redirect required
verify(responseMock, times(1)).sendRedirect(
Matchers.contains(Marketplace.MARKETPLACE_ROOT));
// if mId given, do not use fallback via subscription key or cookie
verify(mpSvcMock, never()).getMarketplaceForSubscription(
Matchers.anyLong(), Matchers.anyString());
verify(requestMock, times(1)).getCookies();
verify(sessionMock, never()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, Any.class);
}
@Test
public void doFilter_noValidMidInGivenUrl_previousmIdStoredInCookieButMpDeletedMeanwhile()
throws Exception {
final String cookieId = fakeSetCookieMid(
Constants.REQ_PARAM_MARKETPLACE_ID, "myMpId");
fakeUserLogin("myOrgId");
fakeCreateMarketplace(cookieId);
when(mpSvcMock.getMarketplaceById(cookieId)).thenThrow(
new ObjectNotFoundException());
when(mpSvcMock.getMarketplaceById("myOrgId")).thenThrow(
new ObjectNotFoundException());
mpCtxFilter.doFilter(requestMock, responseMock, chainMock);
verify(sessionMock, never()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, cookieId);
verify(sessionMock, never()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, Any.class);
assertEquals(null, sessionMid);
}
@Before
public void before() {
GattUtils utilsMock = mock(GattUtils.class);
LowEnergyAclListener lowEnergyAclListenerMock = mock(LowEnergyAclListener.class);
BluetoothAdapter adapterMock = mock(BluetoothAdapter.class);
BluetoothRadioStatusListener bluetoothRadioStatusListenerMock = mock(BluetoothRadioStatusListener.class);
BitGattDependencyProvider dependencyProviderMock = mock(BitGattDependencyProvider.class);
Context mockContext = mock(Context.class);
when(mockContext.getSystemService(Any.class)).thenReturn(null);
when(mockContext.getApplicationContext()).thenReturn(mockContext);
doReturn(bluetoothRadioStatusListenerMock).when(dependencyProviderMock).getNewBluetoothRadioStatusListener(any(), eq(false));
doReturn(utilsMock).when(dependencyProviderMock).getNewGattUtils();
doReturn(lowEnergyAclListenerMock).when(dependencyProviderMock).getNewLowEnergyAclListener();
doReturn(adapterMock).when(utilsMock).getBluetoothAdapter(mockContext);
doReturn(true).when(adapterMock).isEnabled();
Looper mockMainThreadLooper = mock(Looper.class);
Thread mockMainThread = mock(Thread.class);
when(mockMainThread.getName()).thenReturn("Irvin's mock thread");
when(mockMainThreadLooper.getThread()).thenReturn(mockMainThread);
Context ctx = mock(Context.class);
when(ctx.getApplicationContext()).thenReturn(ctx);
when(ctx.getMainLooper()).thenReturn(mockMainThreadLooper);
Handler mockHandler = mock(Handler.class);
doAnswer(handlerPostAnswer).when(mockHandler).post(any(Runnable.class));
doAnswer(handlerPostAnswer).when(mockHandler).postDelayed(any(Runnable.class), anyLong());
when(mockHandler.getLooper()).thenReturn(mockMainThreadLooper);
conn = spy(new GattConnection(device, ctx.getMainLooper()));
conn.setMockMode(true);
when(conn.getMainHandler()).thenReturn(mockHandler);
conn.setState(GattState.IDLE);
FitbitGatt.getInstance().setAsyncOperationThreadWatchdog(mock(LooperWatchdog.class));
FitbitGatt.getInstance().registerGattEventListener(new NoOpGattCallback());
FitbitGatt.getInstance().setDependencyProvider(dependencyProviderMock);
FitbitGatt.getInstance().startGattClient(ctx);
FitbitGatt.getInstance().putConnectionIntoDevices(device, conn);
}
@Test
public void doFilter_noMidInGivenUrl() throws Exception {
mpCtxFilter.doFilter(requestMock, responseMock, chainMock);
verify(mpSvcMock, never()).getMarketplaceById(any(String.class));
verify(sessionMock, never()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, Any.class);
}
@Test
public void doFilter_noValidMidInGivenUrl_noSessionMid_MPLcontext()
throws Exception {
fakeUserLogin("myOrgId");
fakeRequest(null, "/marketplace/index.jsf");
mpCtxFilter.doFilter(requestMock, responseMock, chainMock);
verify(sessionMock, never()).setAttribute(
Constants.REQ_PARAM_MARKETPLACE_ID, Any.class);
}
@Test
public void shouldSerializeObjectMock() throws Exception {
// given
Any mock = mock(Any.class);
// when
ByteArrayOutputStream serialized = serializeMock(mock);
// then
deserializeMock(serialized, Any.class);
}
@Test
public void shouldSerializeRealPartialMock() throws Exception {
// given
Any mock = mock(Any.class, withSettings().serializable());
when(mock.matches(anyObject())).thenCallRealMethod();
// when
ByteArrayOutputStream serialized = serializeMock(mock);
// then
Any readObject = deserializeMock(serialized, Any.class);
readObject.matches("");
}
@Test
public void should_serialize_object_mock() throws Exception {
// when
ByteArrayOutputStream serialized = serializeMock(any);
// then
deserializeMock(serialized, Any.class);
}
@Test
public void should_serialize_real_partial_mock() throws Exception {
// given
when(anyMock.matches(anyObject())).thenCallRealMethod();
// when
ByteArrayOutputStream serialized = serializeMock(anyMock);
// then
Any readObject = deserializeMock(serialized, Any.class);
readObject.matches("");
}
@Test
public void should_serialize_object_mock() throws Exception {
// given
Any mock = mock(Any.class);
// when
ByteArrayOutputStream serialized = serializeMock(mock);
// then
deserializeMock(serialized, Any.class);
}
@Test
public void should_serialize_real_partial_mock() throws Exception {
// given
Any mock = mock(Any.class, withSettings().serializable());
when(mock.matches(anyObject())).thenCallRealMethod();
// when
ByteArrayOutputStream serialized = serializeMock(mock);
// then
Any readObject = deserializeMock(serialized, Any.class);
readObject.matches("");
}