powermockito

How to unit test Log.e in android?

柔情痞子 提交于 2020-01-04 02:45:09
问题 I need to perform an unit test where I need to check if an error message is logged when a certain condition occurs in my app. try { //do something } catch (ClassCastException | IndexOutOfBoundsException e) { Log.e(INFOTAG, "Exception "+e.getMessage()); } How can I test this? I am getting the below error while unit testing. Caused by: java.lang.RuntimeException: Method e in android.util.Log not mocked. 回答1: There are two ways to do this: You turn to Powermock or Powermokito; as those mocking

How to mock constructor with PowerMockito

时间秒杀一切 提交于 2019-12-29 07:44:09
问题 I'm trying to mock a class constructor with PowerMockito for first time, but it doesn't work. My current code is: public class Bar { public String getText() { return "Fail"; } } public class Foo { public String getValue(){ Bar bar= new Bar(); return bar.getText(); } } @RunWith(PowerMockRunner.class) @PrepareForTest(Bar.class) public class FooTest { private Foo foo; @Mock private Bar mockBar; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); PowerMockito

Mocking Chained calls in Concrete Class Mockito

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 09:36:37
问题 I am using Mockito services to test the Exception that could be thrown in MyFinalClass and be caught in MyAbstractClass as the concrete method makes a call to the getObject method in MyFinalClass. Please see below code. public abstract class MyAbstractClass{ @Autowired private MyFinalClass myFinalClass; //concrete method protected Object myConcreteMethod(String name){ try{ Object b = myFinalClass.getObject(name); return b; } catch(MyException e){ LOGGER.log("ERROR THROWN" + e); } } } public

AbstractMethodError when calling PowerMockito.mockStatic

时间秒杀一切 提交于 2019-12-24 03:48:07
问题 I'm trying to use PowerMockito to mock a static call. However, when I try to do so, the PowerMockito.mockStatic call throws an AbstractMethodError: java.lang.AbstractMethodError: org.mockito.configuration.MockitoConfiguration.enableClassCache()Z at org.mockito.internal.configuration.GlobalConfiguration.enableClassCache(GlobalConfiguration.java:63) at org.mockito.internal.creation.instance.ObjenesisInstantiator.<init>(ObjenesisInstantiator.java:11) at org.mockito.internal.creation.instance

Mock android Patterns with mockito

a 夏天 提交于 2019-12-23 20:00:25
问题 I want validate an email with some code provided by Android. Here is the code I want to mock : if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()) throw new InvalidPhoneException(phone); In my test file : @RunWith(PowerMockRunner.class) @PrepareForTest({ Patterns.class }) public class UserTest { @Before public void mockValidator() { mockStatic(Patterns.class); when(Patterns.EMAIL_ADDRESS.matcher(any(String.class)).matches()).thenReturn(true); } I got this error when I launch the tests : java

Always get thread error even if unit test passes in Android using Mockito

和自甴很熟 提交于 2019-12-23 10:28:09
问题 I always get an error even if a test passes and not sure why. This time i was checking there is a null pointer if the view is null. @Before public void setUp() { MockitoAnnotations.initMocks(this); mockView = mock(CollectionContract.View.class); // Get a reference to the class under test presenter = new CollectionPresenter(repository, mockView); } @Test(expected = NullPointerException.class) public void testShowingUIWhenViewIsNull() { presenter = new CollectionPresenter(repository, null);

Mocking System class method using testng, mockito and powermock

只愿长相守 提交于 2019-12-23 05:34:11
问题 I am currently writing tests to a legacy code which uses a function System.getenv("some_environment_variable") I get a problem when I try to mock these variables by using mockito and powermock (used under testng framework) What I did so far was @BeforeClass public void setup() { PowerMockito.mockStatic(System.class); PowerMockito.when(System.getenv("hello")).thenReturn("world"); } @Test public void test() { assertEquals(System.getenv("hello"), "world"); } But when I tried to run the code

No methods matching the name(s) stream in hierarchy of class java.util.Arrays$ArrayList

早过忘川 提交于 2019-12-23 05:27:37
问题 So I am using java 8 and trying to write some tests with PowerMock and Mockito. I am getting a MethodNotFoundException with the message: No methods matching the name(s) stream were found in the class hierarchy of class java.util.Arrays$ArrayList. I double checked the ArrayList documentation and it definitely looks like it inherits stream from Collections. Is this a problem with PowerMockito or am I missing something? Line in question PowerMockito.when(thing.call("services", "things"))

Powermock static final method in final class

泪湿孤枕 提交于 2019-12-22 09:46:37
问题 The Test case I am writing for: public class AClassUnderTest { // This test class has a method call public Long methodUnderTest() { // Uses the FinalUtilityClass which contains static final method FinalUtilityClass.myStaticFinalMethod(<3-parameters-here>); // I want to mock above call so that test case for my "methodUnderTest" passes } } I have one final class. public final class FinalUtilityClass { /** * Method has 3 parameters */ public static final MyBean myStaticFinalMethod(<3-parameters

PowerMockito .when().thenReturn() with randomUUID not returning expected value [duplicate]

做~自己de王妃 提交于 2019-12-22 08:29:33
问题 This question already has answers here : How do I unit test code which uses Java UUID? (4 answers) Closed 2 years ago . I'm trying to test a Web Service method which connects to a SQL Server Database which contains JCR nodes, as we're using JackRabbit. The method looks like: public String addDocumentByJson(String fileName, byte[] fileContent, int status, String userName, String jsonProperties) { UUID id = UUID.randomUUID(); // It does a bunch of operations here return jsonResult; } Where