powermock

apkbuilder finds duplicate file when adding powermock to an android test project

匆匆过客 提交于 2020-01-02 04:40:11
问题 I'm attempting to add powermock-mockito and mockito into an Android Test project. I created the android test project using the android command-line tool to create the build.xml and project structure. I have the following jars in my libs/ directory: dexmaker-1.0.jar dexmaker-mockito-1.0.jar mockito-all-1.9.5.jar powermock-mockito-1.5-full.jar When I attempt to build the project with ant debug, I get the following error: [apkbuilder] Creating ProjectTests-debug-unaligned.apk and signing it with

Powermock JUnit tests are taking more time to execute compared to normal JUnit

a 夏天 提交于 2020-01-02 04:06:07
问题 we are using powermock for mocking the static methods by using the @PrepareForTest annotations. The test runs fine but the problem is it is taking more time to execute the test. The code snippet is like below: @PrepareForTest({ StaticClass1.class, StaticClass2.class }) Normally, the JUnit with out mocking the static method is taking around 2 sec time to execute but when we add @PrepareForTest annotation for mocking the static calls, the test is taking around a minute time to complete the

How to mock a private dao variable?

你。 提交于 2020-01-02 01:43:05
问题 I have a dao.create() call that I want to mock when testing a method. But I am missing something as I'm still getting NPE. What is wrong here? class MyService { @Inject private Dao dao; public void myMethod() { //.. dao.create(object); // } } How can I mock out the dao.create() call? @RunWith(PowerMockRunner.class) @PrepareForTest(DAO.class) public void MyServiceTest { @Test public void testMyMethod() { PowerMockito.mock(DAO.class); MyService service = new MyService(); service.myMethod(); /

How to mock non static methods using PowerMock

妖精的绣舞 提交于 2020-01-01 05:28:11
问题 I am trying to mock an inner method call of my test method My class looks like this public class App { public Student getStudent() { MyDAO dao = new MyDAO(); return dao.getStudentDetails();//getStudentDetails is a public //non-static method in the DAO class } When I write the junit for the method getStudent(), is there a way in PowerMock to mock the line dao.getStudentDetails(); or make the App class use a mock dao object during junit execution instead of the actual dao call which connects to

Mocking getClass method with PowerMockito

谁说胖子不能爱 提交于 2019-12-30 18:30:20
问题 I'd like to avoid mocking the getClass() method for a class but cannot seem to find any way around it. I'm trying to test a class that stores objects class types in a HashMap to a particular method to be used later. A brief example of this is: public class ClassToTest { /** Map that will be populated with objects during constructor */ private Map<Class<?>, Method> map = new HashMap<Class<?>, Method>(); ClassToTest() { /* Loop through methods in ClassToTest and if they return a boolean and

How mock private method that modify private variables?

…衆ロ難τιáo~ 提交于 2019-12-30 10:19:27
问题 How mock private method that modify private variables? class SomeClass{ private int one; private int second; public SomeClass(){} public int calculateSomething(){ complexInitialization(); return this.one + this.second; } private void complexInitialization(){ one = ... second = ... } } 回答1: You don't , because your test will depend on implementation details of the class it is testing and will therefore be brittle. You could refactor your code such that the class you are currently testing

How to use PowerMock in Android projects?

♀尐吖头ヾ 提交于 2019-12-30 04:01:10
问题 I created a new Android test project. I downloaded powermock-mockito-junit-1-1.5.zip from https://code.google.com/p/powermock/downloads/list. I added all of the libraries to the test project's libs folder. The test class is a very simple object: package com.test.test; import org.junit.runner.RunWith; import org.powermock.modules.junit4.PowerMockRunner; import android.util.Log; @RunWith(PowerMockRunner.class) public class TestTestAndroid { public void testRuns() { Log.e("test", "Test case is

How to android unit test and mock a static method

倖福魔咒の 提交于 2019-12-29 05:19:30
问题 Hi I really hope you can help me, I feel like I've been pulling my hair out for days. I'm trying to write unit tests for a method A. Method A calls a static method B. I want to mock static method B. I know this has been asked before, but I feel Android has matured since then, and there must be a way to do such a simple task without re-writing the methods I want to test. Here is an example, first the method I want to test: public String getUserName(Context context, HelperUtils helper) { if

How to android unit test and mock a static method

独自空忆成欢 提交于 2019-12-29 05:19:09
问题 Hi I really hope you can help me, I feel like I've been pulling my hair out for days. I'm trying to write unit tests for a method A. Method A calls a static method B. I want to mock static method B. I know this has been asked before, but I feel Android has matured since then, and there must be a way to do such a simple task without re-writing the methods I want to test. Here is an example, first the method I want to test: public String getUserName(Context context, HelperUtils helper) { if

PowerMockito使用详解

限于喜欢 提交于 2019-12-26 04:52:42
  一、 PowerMock概述 现如今比较流行的Mock工具如jMock,EasyMock,Mockito等都有一个共同的缺点:不能mock静态、final、私有方法等。而PowerMock能够完美的弥补以上三个Mock工具的不足。 PowerMock是一个扩展了其它如EasyMock等mock框架的、功能更加强大的框架。PowerMock使用一个自定义类加载器和字节码操作来模拟静态方法,构造函数,final类和方法,私有方法,去除静态初始化器等等。通过使用自定义的类加载器,简化采用的IDE或持续集成服务器不需要做任何改变。熟悉PowerMock支持的mock框架的开发人员会发现PowerMock很容易使用,因为对于静态方法和构造器来说,整个的期望API是一样的。PowerMock旨在用少量的方法和注解扩展现有的API来实现额外的功能。目前PowerMock支持EasyMock和Mockito。 二 、 PowerMock 入门 PowerMock有两个重要的注解: –@RunWith(PowerMockRunner.class) –@PrepareForTest( { YourClassWithEgStaticMethod.class }) 如果你的测试用例里没有使用注解@PrepareForTest,那么可以不用加注解@RunWith(PowerMockRunner