powermock

How to mock getResourceAsStream method using PowerMockito and JUnit?

断了今生、忘了曾经 提交于 2020-01-24 10:11:05
问题 I try to mock getResourceAsStream method which I invoke in constructor. public Conn() { stream = Conn.class.getClass().getResourceAsStream(PATH); } For mock framework I prefer Mockito + PowerMockito. @RunWith(PowerMockRunner.class) @PrepareForTest(Conn.class) public class ConnTest { @Mock private InputStream streamMock; private Conn conn; @Before public void before() { initMocks(this); } @Test public void test() { PowerMockito.mockStatic(Conn.class); PowerMockito.when(Connector.class

Mocking static private final variable using Powermock?

爷,独闯天下 提交于 2020-01-24 09:44:05
问题 I have a utility class which is a final class. There i have used injection for injecting LOGGER. public final class Utilities { @Inject private static Logger.ALogger LOGGER; private Utilities() { //this is the default constructor. so there is no implementation } public static String convertToURl(string input){ try{ //do some job }catch(IllegalArgumentException ex){ LOGGER.error("Invalid Format", ex); } } } While I writing unit testing for this method i have to mock LOGGER otherwise it will

How can I mock a void method to throw an exception?

戏子无情 提交于 2020-01-23 04:31:07
问题 I have a structure like this: public class CacheWrapper { private Map<Object, Object> innerMap; public CacheWrapper() { //initialize the innerMap with an instance for an in-memory cache //that works on external server //current implementation is not relevant for the problem innerMap = ...; } public void putInSharedMemory(Object key, Object value) { innerMap.put(key, value); } public Object getFromSharedMemory(Object key) { return innerMap.get(key); } } And my client class (you could say it

How can I test a method which invoke protected (unwanted) methods of parent class?

旧时模样 提交于 2020-01-22 15:40:14
问题 I'm stuck in a very weird case. I have some specific code which I need to test. Here it is: public class A { /* * The real method of real class is so big that I just don't want to test it. * That's why I use throwing an exception. */ protected void method(Integer result) { throw new RuntimeException("Oops!"); } protected <T> T generifiedMethod(String s, T type) { throw new RuntimeException("Oops!"); } protected void mainMethod(Integer value) { throw new RuntimeException("Oops!"); } } I also

How could I test ClassNotFoundException using Mockito?

本秂侑毒 提交于 2020-01-16 10:50:26
问题 I'm running unit test on the following class: public class FileClass { public void funcB() { try { throw new ClassNotFoundException(); } catch( ClassNotFoundException e ) { } } } Using Mockito code as shown below: public class TestFileClass { @Test(expected=ClassNotFoundException.class) public void testFuncB() { FileClass fc = Mockito.spy(new FileClass()); fc.funcB(); } } But the test were failed due to following error: java.lang.AssertionError: Expected exception: java.lang

SecretKeyFactory.getInstance() throws exception for all algorithms in unit tests

主宰稳场 提交于 2020-01-14 09:17:14
问题 By some reason I always get exception in unit test when calling SecretKeyFactory.getInstance() no matter what algorithm is specified. For example: SecretKeyFactory.getInstance("PBEWITHMD5ANDDES") com.mhe.connect.util.EncryptionException: java.security.NoSuchAlgorithmException: PBEWITHMD5ANDDES SecretKeyFactory not available At the same time, I see that Security.getProviders() returns me needed algorithms: SECRETKEYFACTORY.DESEDE SunJCE SECRETKEYFACTORY.PBEWITHMD5ANDDES SunJCE SECRETKEYFACTORY

How to suppress and verify private static method calls?

强颜欢笑 提交于 2020-01-14 08:43:28
问题 I am currently stumbling in JUnit testing and need some help. So I got this class with static methods which will refactor some objects. For simplification's sake I have made a small example. This is my Factory class: class Factory { public static String factorObject() throws Exception { String s = "Hello Mary Lou"; checkString(s); return s; } private static void checkString(String s) throws Exception { throw new Exception(); } } And this is my Test class: @RunWith(PowerMockRunner.class)

How to suppress and verify private static method calls?

浪尽此生 提交于 2020-01-14 08:43:06
问题 I am currently stumbling in JUnit testing and need some help. So I got this class with static methods which will refactor some objects. For simplification's sake I have made a small example. This is my Factory class: class Factory { public static String factorObject() throws Exception { String s = "Hello Mary Lou"; checkString(s); return s; } private static void checkString(String s) throws Exception { throw new Exception(); } } And this is my Test class: @RunWith(PowerMockRunner.class)

Verify a static method was called by another static method in PowerMock

依然范特西╮ 提交于 2020-01-14 05:28:11
问题 I have a Tool class with two static methods, doSomething(Object) and callDoSomething(). The names are intuitive in that callDoSomething delegates its call to doSomething(Object); public class Tool { public static void doSomething( Object o ) { } public static void callDoSomething() { doSomething( new Object()); } } I have a Test class for Tool and I'd like to verify if doSomething(Object) was called (I want to do Argument Matching too in the future) @RunWith( PowerMockRunner.class )

Testing code which calls native methods

痴心易碎 提交于 2020-01-12 07:10:11
问题 I have a class like this: public final class Foo { public native int getBar(); public String toString() { return "Bar: " + getBar(); } } Please note that getBar() is implemented with JNI and that the class is final . I want to write a junit test to test the toString() method. For this I need to mock the getBar() method and then run the original toString() method to check the output. My first thought was that this must be impossible but then I found PowerMock which supports testing final