@Test @SuppressWarnings("unchecked") public void testListFilesNotFile() throws Exception { final Class<?> optionsClass = Class.forName(Main.class.getName()); final Method method = optionsClass.getDeclaredMethod("listFiles", File.class, List.class); method.setAccessible(true); final File fileMock = mock(File.class); when(fileMock.canRead()).thenReturn(true); when(fileMock.isDirectory()).thenReturn(false); when(fileMock.isFile()).thenReturn(false); final List<File> result = (List<File>) method.invoke(null, fileMock, null); assertEquals("Invalid result size", 0, result.size()); }
@Override protected AbstractPartitionDiscoverer createPartitionDiscoverer( KafkaTopicsDescriptor topicsDescriptor, int indexOfThisSubtask, int numParallelSubtasks) { AbstractPartitionDiscoverer mockPartitionDiscoverer = mock(AbstractPartitionDiscoverer.class); try { when(mockPartitionDiscoverer.discoverPartitions()).thenReturn(partitions); } catch (Exception e) { // ignore } when(mockPartitionDiscoverer.setAndCheckDiscoveredPartition(any(KafkaTopicPartition.class))).thenReturn(true); return mockPartitionDiscoverer; }
private void setupShellMocks(String username, List<String> groups) throws IOException { PowerMockito.mockStatic(CommonUtils.class); PowerMockito.when(CommonUtils.getUnixGroups(eq(username))).thenReturn(groups); }
@Test public void loadConfigWhenDefaultConfigurerAsSpringFactoryhenDefaultConfigurerApplied() { spy(SpringFactoriesLoader.class); DefaultConfigurer configurer = new DefaultConfigurer(); when(SpringFactoriesLoader .loadFactories(AbstractHttpConfigurer.class, getClass().getClassLoader())) .thenReturn(Arrays.<AbstractHttpConfigurer>asList(configurer)); loadConfig(Config.class); assertThat(configurer.init).isTrue(); assertThat(configurer.configure).isTrue(); }
@Test public void testGetAttributesXml_DefaultTag_EmptyMap() { PowerMockito.when( AttributesUtil.getAttributesXml( any( Map.class ) ) ).thenCallRealMethod(); PowerMockito.when( AttributesUtil.getAttributesXml( any( Map.class ), anyString() ) ).thenCallRealMethod(); Map<String, Map<String, String>> attributesMap = new HashMap<>(); String attributesXml = AttributesUtil.getAttributesXml( attributesMap ); assertNotNull( attributesXml ); // Check that it's not an empty XML fragment assertTrue( attributesXml.contains( AttributesUtil.XML_TAG ) ); }
@Test public void testExceptionNoSuchAlgorithmException() throws Exception { final Configuration config = new DefaultConfiguration("myName"); final String filePath = temporaryFolder.newFile().getPath(); final PropertyCacheFile cache = new PropertyCacheFile(config, filePath); cache.put("myFile", 1); mockStatic(MessageDigest.class); when(MessageDigest.getInstance("SHA-1")) .thenThrow(NoSuchAlgorithmException.class); final Class<?>[] param = new Class<?>[1]; param[0] = Serializable.class; final Method method = PropertyCacheFile.class.getDeclaredMethod("getHashCodeBasedOnObjectContent", param); method.setAccessible(true); try { method.invoke(cache, config); fail("InvocationTargetException is expected"); } catch (InvocationTargetException ex) { assertTrue("Invalid exception cause", ex.getCause().getCause() instanceof NoSuchAlgorithmException); assertEquals("Invalid exception message", "Unable to calculate hashcode.", ex.getCause().getMessage()); } }
@Test public void testPackagesWithIoExceptionGetResources() throws Exception { final ClassLoader classLoader = mock(ClassLoader.class); when(classLoader.getResources("checkstyle_packages.xml")).thenThrow(IOException.class); try { PackageNamesLoader.getPackageNames(classLoader); fail("CheckstyleException is expected"); } catch (CheckstyleException ex) { assertTrue("Invalid exception cause class", ex.getCause() instanceof IOException); assertEquals("Invalid exception message", "unable to get package file resources", ex.getMessage()); } }
public static RectangleWrap createRectangleMock(int width, int height) { RectangleWrap r = mock(RectangleWrap.class); r.width = width; r.height = height; when(r.width()).thenReturn(width); when(r.height()).thenReturn(height); return r; }
private void setUpPresenter() throws Exception { mockPresenter = mock(TestPresenter.class); mockDelegate = mock(PresenterLifecycleDelegate.class); PowerMockito.whenNew(PresenterLifecycleDelegate.class).withAnyArguments().thenReturn(mockDelegate); when(mockDelegate.getPresenter()).thenReturn(mockPresenter); mockFactory = mock(ReflectionPresenterFactory.class); when(mockFactory.createPresenter()).thenReturn(mockPresenter); PowerMockito.mockStatic(ReflectionPresenterFactory.class); when(ReflectionPresenterFactory.fromViewClass(any(Class.class))).thenReturn(mockFactory); }
@Test @SuppressWarnings("unchecked") public void testListFilesDirectoryWithNull() throws Exception { final Class<?> optionsClass = Class.forName(Main.class.getName()); final Method method = optionsClass.getDeclaredMethod("listFiles", File.class, List.class); method.setAccessible(true); final File fileMock = mock(File.class); when(fileMock.canRead()).thenReturn(true); when(fileMock.isDirectory()).thenReturn(true); when(fileMock.listFiles()).thenReturn(null); final List<File> result = (List<File>) method.invoke(null, fileMock, new ArrayList<Pattern>()); assertEquals("Invalid result size", 0, result.size()); }
@Test public void createExpressionMessageMetadataSourceMatchFirst() { when(matcher1.matches(message)).thenReturn(true); Collection<ConfigAttribute> attrs = source.getAttributes(message); assertThat(attrs).hasSize(1); ConfigAttribute attr = attrs.iterator().next(); assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class); assertThat( ((MessageExpressionConfigAttribute) attr).getAuthorizeExpression() .getValue(rootObject)).isEqualTo(true); }
@Test public void testGetAttributesXml_DefaultTag_NullParameter() { PowerMockito.when( AttributesUtil.getAttributesXml( any( Map.class ) ) ).thenCallRealMethod(); PowerMockito.when( AttributesUtil.getAttributesXml( any( Map.class ), anyString() ) ).thenCallRealMethod(); String attributesXml = AttributesUtil.getAttributesXml( null ); assertNotNull( attributesXml ); // Check that it's not an empty XML fragment assertTrue( attributesXml.contains( AttributesUtil.XML_TAG ) ); }
private void mockCustomTabsAllowed(final boolean allowed) { final FetchedAppSettings settings = mock(FetchedAppSettings.class); when(settings.getCustomTabsEnabled()).thenReturn(allowed); mockStatic(FetchedAppSettingsManager.class); when(FetchedAppSettingsManager.getAppSettingsWithoutQuery(anyString())).thenReturn(settings); }
private ColorWrap createColorMock(int i) { ColorWrap c = mock(ColorWrap.class); when(c.getColorValue()).thenReturn(i); return c; }
private void setUpPresenter() throws Exception { mockPresenter = mock(TestPresenter.class); mockDelegate = mock(PresenterLifecycleDelegate.class); PowerMockito.whenNew(PresenterLifecycleDelegate.class).withAnyArguments().thenReturn(mockDelegate); when(mockDelegate.getPresenter()).thenReturn(mockPresenter); mockFactory = mock(ReflectionPresenterFactory.class); when(mockFactory.createPresenter()).thenReturn(mockPresenter); PowerMockito.mockStatic(ReflectionPresenterFactory.class); when(ReflectionPresenterFactory.fromViewClass(any(Class.class))).thenReturn(mockFactory); }
@Before public void setup() { mockStatic(Utility.class); mMockActivity = mock(Activity.class); mMockPackageManager = mock(PackageManager.class); when(mMockActivity.getPackageManager()).thenReturn(mMockPackageManager); }
@Test public void createExpressionMessageMetadataSourceMatchSecond() { when(matcher2.matches(message)).thenReturn(true); Collection<ConfigAttribute> attrs = source.getAttributes(message); assertThat(attrs).hasSize(1); ConfigAttribute attr = attrs.iterator().next(); assertThat(attr).isInstanceOf(MessageExpressionConfigAttribute.class); assertThat( ((MessageExpressionConfigAttribute) attr).getAuthorizeExpression() .getValue(rootObject)).isEqualTo(false); } }
@Test public void testGetAttributesXml_CustomTag_EmptyMap() { PowerMockito.when( AttributesUtil.getAttributesXml( any( Map.class ), anyString() ) ).thenCallRealMethod(); Map<String, Map<String, String>> attributesMap = new HashMap<>(); String attributesXml = AttributesUtil.getAttributesXml( attributesMap, CUSTOM_TAG ); assertNotNull( attributesXml ); // Check that it's not an empty XML fragment assertTrue( attributesXml.contains( CUSTOM_TAG ) ); }
private void mockTryAuthorize() { mockStatic(FacebookSdk.class); when(FacebookSdk.isInitialized()).thenReturn(true); mockStatic(AccessToken.class); when(AccessToken.getCurrentAccessToken()).thenReturn(null); Fragment fragment = mock(LoginFragment.class); when(mockLoginClient.getFragment()).thenReturn(fragment); }
@Before public void before() throws Exception { mContext = PowerMockito.mock(FileSystemContext.class); mAddress = mock(WorkerNetAddress.class); mClient = mock(BlockWorkerClient.class); mRequestObserver = mock(ClientCallStreamObserver.class); PowerMockito.when(mContext.acquireBlockWorkerClient(mAddress)).thenReturn(mClient); PowerMockito.when(mContext.getClientContext()) .thenReturn(ClientContext.create(mConf)); PowerMockito.when(mContext.getConf()).thenReturn(mConf); PowerMockito.doNothing().when(mContext).releaseBlockWorkerClient(mAddress, mClient); PowerMockito.when(mClient.writeBlock(any(StreamObserver.class))).thenReturn(mRequestObserver); PowerMockito.when(mRequestObserver.isReady()).thenReturn(true); }