本文整理汇总了Java中org.mockito.Mockito.mock方法的典型用法代码示例。如果您正苦于以下问题:Java Mockito.mock方法的具体用法?Java Mockito.mock怎么用?Java Mockito.mock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.mockito.Mockito
的用法示例。
在下文中一共展示了Mockito.mock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testMissingHostname
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void testMissingHostname() throws Exception {
ServletRequest request = Mockito.mock(ServletRequest.class);
Mockito.when(request.getRemoteAddr()).thenReturn(null);
ServletResponse response = Mockito.mock(ServletResponse.class);
final AtomicBoolean invoked = new AtomicBoolean();
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
assertTrue(HostnameFilter.get().contains("???"));
invoked.set(true);
}
};
Filter filter = new HostnameFilter();
filter.init(null);
assertNull(HostnameFilter.get());
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
assertNull(HostnameFilter.get());
filter.destroy();
}
示例2: testSubscribe
import org.mockito.Mockito; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void testSubscribe() throws Exception {
Consumer<Integer> callback = Mockito.mock(Consumer.class);
Consumer<Integer> callback2 = Mockito.mock(Consumer.class);
store = new RxStore<>(initialState, this::reducer);
store.subscribe(callback);
verify(callback, Mockito.never()).accept(any());
verify(callback2, Mockito.never()).accept(any());
store.dispatch(INC);
verify(callback).accept(initialState + 1);
verify(callback2, Mockito.never()).accept(any());
store.subscribe(callback2);
store.dispatch(INC);
verify(callback).accept(initialState + 2);
verify(callback2).accept(initialState + 2);
store.dispatch(DEC);
verify(callback, times(2)).accept(initialState + 1);
verify(callback2).accept(initialState + 1);
assertEquals("The state at the end should not be influenced by the subscriptions", initialState + 1, store.getState().intValue());
}
示例3: returns_compilation_result_when_build_success
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void returns_compilation_result_when_build_success() throws IOException {
final String workspace = folder.newFolder().getAbsolutePath();
final Path workspacePath = Paths.get(workspace);
final String targetDirectory = Paths.get(workspace, "InvalidClassUnderCompilationTest.java").toString();
final InternalCompiler compiler = Mockito.mock(InternalCompiler.class);
Mockito.when(compiler.compile(any(), any(), any()))
.thenReturn(CompilationResult.compilationSucceeded(workspacePath));
this.compiler.init(workspace, Mockito.mock(DependencyResolver.class), compiler);
final String javaFile = ResourceUtils.getFileFromClassPath("java/InvalidClassUnderCompilationTest.java");
final CompilationResult result = this.compiler.compile(javaFile, null);
assertTrue(result.isSuccess());
assertEquals(workspacePath, result.getPath());
Mockito.verify(compiler).compile(new File(javaFile), targetDirectory, Arrays.asList("-d", targetDirectory));
}
示例4: testValidationNegativeCustomPattern
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void testValidationNegativeCustomPattern() throws Exception {
PropertyHandler ph = Mockito.mock(PropertyHandler.class);
Mockito.when(ph.getStackName()).thenReturn("Test123");
Mockito.when(ph.getStackNamePattern()).thenReturn("Test");
try {
validateStackName(ph);
fail();
} catch (APPlatformException e) {
}
}
示例5: whenVoidMethodNoArgsButWithParamsInSignatureThenThrowsException
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void whenVoidMethodNoArgsButWithParamsInSignatureThenThrowsException() throws Exception {
expectedEx.expect(IllegalStateException.class);
expectedEx.expectMessage(Matchers.startsWith("Call proceed without parameters on method"));
MethodSignature signature = Mockito.mock(MethodSignature.class);
when(signature.getParameterTypes()).thenReturn(new Class<?>[] {String.class});
LepMethod method = Mockito.mock(LepMethod.class);
when(method.getMethodSignature()).thenReturn(signature);
TargetProceedingLep proceedingLep = new TargetProceedingLep(method, null);
proceedingLep.proceed();
}
示例6: testInitialize
import org.mockito.Mockito; //导入方法依赖的package包/类
@Override
@Before
public void testInitialize() throws Exception{
super.testInitialize();
ApiFactoryService apiFactoryService = Mockito.mock(ApiFactoryService.class);
Mockito.when(apiFactoryService.syncsPolicyMapping("NSM")).thenReturn(true);
}
示例7: mockClassLoaderCache
import org.mockito.Mockito; //导入方法依赖的package包/类
static ClassLoaderCache mockClassLoaderCache() throws NoSuchFieldException, IllegalAccessException {
ClassLoaderCache mockedClassLoaderCache = Mockito.mock(ClassLoaderCache.class);
Mockito.when(mockedClassLoaderCache.currentClassLoader()).thenReturn(Thread.currentThread().getContextClassLoader());
Field instance = ClassLoaderCache.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(null, mockedClassLoaderCache);
return mockedClassLoaderCache;
}
示例8: setUp
import org.mockito.Mockito; //导入方法依赖的package包/类
@Before
public void setUp() throws JSONException, BlockLoadingException {
mMockController = Mockito.mock(BlocklyController.class);
factory = new BlockFactory();
factory.setController(mMockController);
factory.addDefinition(new BlockDefinition("{\"type\": \"dummyBlock\"}"));
dummyBlock = new BlockTemplate().ofType("dummyBlock");
shadowBlock = new BlockTemplate(dummyBlock).shadow();
input = new Connection(Connection.CONNECTION_TYPE_INPUT, null);
input.setBlock(factory.obtainBlockFrom(dummyBlock));
output = new Connection(CONNECTION_TYPE_OUTPUT, null);
output.setBlock(factory.obtainBlockFrom(dummyBlock));
previous = new Connection(Connection.CONNECTION_TYPE_PREVIOUS, null);
previous.setBlock(factory.obtainBlockFrom(dummyBlock));
next = new Connection(Connection.CONNECTION_TYPE_NEXT, null);
next.setBlock(factory.obtainBlockFrom(dummyBlock));
shadowInput = new Connection(Connection.CONNECTION_TYPE_INPUT, null);
shadowInput.setBlock(factory.obtainBlockFrom(shadowBlock));
shadowOutput = new Connection(CONNECTION_TYPE_OUTPUT, null);
shadowOutput.setBlock(factory.obtainBlockFrom(shadowBlock));
shadowPrevious = new Connection(Connection.CONNECTION_TYPE_PREVIOUS, null);
shadowPrevious.setBlock(factory.obtainBlockFrom(shadowBlock));
shadowNext = new Connection(Connection.CONNECTION_TYPE_NEXT, null);
shadowNext.setBlock(factory.obtainBlockFrom(shadowBlock));
}
示例9: createInvoice
import org.mockito.Mockito; //导入方法依赖的package包/类
public static Invoice createInvoice(DateTime createdDate, LocalDate invoiceDate,
List<InvoiceItem> invoiceItems) {
UUID id = UUID.randomUUID();
Invoice invoice = Mockito.mock(Invoice.class,
Mockito.withSettings().defaultAnswer(RETURNS_SMART_NULLS.get()));
when(invoice.getId()).thenReturn(id);
when(invoice.getCreatedDate()).thenReturn(createdDate);
when(invoice.getInvoiceDate()).thenReturn(invoiceDate);
when(invoice.getInvoiceItems()).thenReturn(invoiceItems);
return invoice;
}
示例10: testBUG401Mockito
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
@Category(value=UnitTest.class)
public void testBUG401Mockito() {
Random mock = Mockito.mock(Random.class);
// rehearse
when(mock.nextInt(7)).thenReturn(5);
// run test
Die die = new JavaRandomDie(mock);
Die copyDie = die.roll();
assertThat(copyDie.getPips()).isGreaterThan(0).isLessThan(7);
}
示例11: testMessageMapperIsEmptyByDefaultAndSupportsMapping
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void testMessageMapperIsEmptyByDefaultAndSupportsMapping() {
SingleModelMessageMapper<Integer> singleModelMessageMapper =
() -> "mediaType";
HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
SingleModel<Integer> singleModel = new SingleModel<>(3, Integer.class);
assertThat(
singleModelMessageMapper.supports(singleModel, httpHeaders),
is(true));
}
示例12: getElement
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void getElement() {
RskCustomCache cache = new RskCustomCache(TIME_TO_LIVE);
BlockHeader header1 = Mockito.mock(BlockHeader.class);
cache.put(KEY, header1);
Assert.assertNotNull(cache.get(KEY));
Assert.assertNull(cache.get(OTHER_KEY));
}
示例13: checkForwardTo
import org.mockito.Mockito; //导入方法依赖的package包/类
/**
* Test use case forward.
*/
private void checkForwardTo(final String from, final String to, final String suffix) throws IOException, ServletException {
final HtmlProxyFilter htmlProxyFilter = new HtmlProxyFilter();
htmlProxyFilter.setSuffix(suffix);
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.when(request.getServletPath()).thenReturn(from);
final RequestDispatcher requestDispatcher = Mockito.mock(RequestDispatcher.class);
Mockito.when(request.getRequestDispatcher(to)).thenReturn(requestDispatcher);
htmlProxyFilter.doFilter(request, response, null);
Mockito.verify(requestDispatcher, Mockito.atLeastOnce()).forward(request, response);
Mockito.validateMockitoUsage();
}
示例14: testInitSpring
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void testInitSpring() {
boolean status = true;
RestServletContextListener listener = new RestServletContextListener();
ServletContextEvent sce = Mockito.mock(ServletContextEvent.class);
ServletContext context = Mockito.mock(ServletContext.class);
Mockito.when(sce.getServletContext()).thenReturn(context);
Mockito.when(sce.getServletContext().getInitParameter("contextConfigLocation")).thenReturn("locations");
try {
listener.initSpring(sce);
} catch (Exception e) {
status = false;
}
Assert.assertFalse(status);
}
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:16,代码来源:TestRestServletContextListener.java
示例15: onResponseContent
import org.mockito.Mockito; //导入方法依赖的package包/类
@Test
public void onResponseContent() throws IOException {
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
final ServletOutputStream outputStream = Mockito.mock(ServletOutputStream.class);
Mockito.when(response.getOutputStream()).thenReturn(outputStream);
final Response proxyResponse = Mockito.mock(Response.class);
servlet.onResponseContent(new HttpServletRequestWrapper(request), response, proxyResponse, null, 0, 0, callback);
Mockito.verify(outputStream, Mockito.times(1)).write(null, 0, 0);
}