powermock

Mockito: Mock class<T> object

孤者浪人 提交于 2019-12-10 23:07:48
问题 Apologies if it's already been discussed but I didn't find any solutions. Problem - Trying to mock an object of my Class of some type (e.g. class) Writing test case of method xyz() where i need to mock SomeClass.class as mentioned in below code snippets void xyz() { .. MyOtherClass.staticMethod(SomeClass.class); .. } MyOtherClass { .. <T> T staticMethod(Class<T> clazz) { } ... } Using power mockito Tried (Class) Mockito.mock(Class.class), which is not working. I hope above code clears the

Mocking an abstract class and injecting classes with Mockito annotations?

余生颓废 提交于 2019-12-10 22:43:09
问题 Is it possible to both mock an abstract class and inject it with mocked classes using Mockito annotations. I now have the following situation: @Mock private MockClassA mockClassA; @Mock private MockClassB mockClassB; @Mock(answer = Answers.CALLS_REAL_METHODS) private AbstractClassUnderTest abstractClassUnderTest; @Before public void init() { MockitoAnnotations.initMocks(this); Whitebox.setInternalState(abstractClassUnderTest, mockClassA); Whitebox.setInternalState(abstractClassUnderTest,

PowerMock - Mock a Singleton with a Private Constructor

此生再无相见时 提交于 2019-12-10 22:42:32
问题 I'm using PowerMock with EasyMock, and wondered how I might mock a singleton with a private constructor? Let's say I have the following class: public class Singleton { private static Singleton singleton = new Singleton(); private Singleton() { } public static Singleton getInstance() { return singleton; } public int crazyServerStuff() { ... } } And a class which uses this: public class Thing { public Thing() {} public int doStuff(Singleton s) { return s.crazyServerStuff() + 42; } } How might I

Emma code coverage with JUnit and Powermock

£可爱£侵袭症+ 提交于 2019-12-10 21:31:31
问题 I am using JUnit with Powermockito mocking. I have to work on a CLI environment with maven or ant. emma version: ema-2.0.5312 powermock version: powermock-mockito-1.5.1-full junit version: junit-4.9 When I run junit through the following command, everything works find: java org.junit.runner.JUnitCore some.package.ClassTest However, when I used emma to check the code coverage: java emmarun -cp $CLASSPATH -report txt org.junit.runner.JUnitCore some.package.ClassTest I got the following error: 1

Dependencies while implementing Mocking in Junit4 (PowerMock)

拟墨画扇 提交于 2019-12-10 18:38:42
问题 Below is my JUnit test case: import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Date; import java.util.HashMap; import static org.easymock.EasyMock.expect; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.annotation.Mock; import org.powermock.core.classloader.annotations.PrepareForTest; import

Mocking Hibernate Session

為{幸葍}努か 提交于 2019-12-10 18:37:07
问题 I was trying to mock hibernate session. This is the code snippet I tried: @Before public void setUp() { campaignModel = DraftTestHelper.buildDraftModel(); if(sessionFactory != null) { System.out.println("Session Factory not null"); } else System.out.println("Session Factory is null"); session = sessionFactory.getCurrentSession(); if(session != null) { System.out.print("Not null"); } else System.out.println("Null"); } Mock Code: @Mock SessionFactory sessionFactory; @InjectMocks

Mocking a method returns null

折月煮酒 提交于 2019-12-10 17:21:35
问题 I have the following method public ResultScanner getScanner(Scan scan) { Table table = getTableInstance("Sampletable"); return table.getScanner(scan); } For this I have written following junit test code Connection mockconnection = PowerMockito.mock(Connection.class); Table mocktable = PowerMockito.mock(Table.class); PowerMockito.when(mockconnection.getTable(TableName.valueOf(Mockito.anyString()))) .thenReturn(mocktable); Scan mockedScan = PowerMockito.mock(Scan.class); ResultScanner mockrs =

mockito + easymock - NoClassDefFoundError:net/sf/cglib/proxy/Enhancer

☆樱花仙子☆ 提交于 2019-12-10 14:31:34
问题 I am writing a test using easymock + powermock, the reason I am using powermock is to mock a static method. my test class looks like: import static org.powermock.api.easymock.PowerMock.mockStatic; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(StaticTest.class) public

jacoco: For report generation the same class files must be used as at runtime

拈花ヽ惹草 提交于 2019-12-10 13:56:34
问题 I have been working on the android project and use roboletric and powermock to do the unitTest. When I run gradle jacocoTestReport , it will show [ant:jacocoReport] Classes in bundle 'app' do no match with execution data. For report generation the same class files must be used as at runtime. [ant:jacocoReport] Execution data for class com/my/app/MyClass does not match. Where I use powermock to mock the static method in Myclass.java @RunWith(RobolectricGradleTestRunner.class) @Config(constants

PowerMock test passes then fails

断了今生、忘了曾经 提交于 2019-12-10 13:54:18
问题 Why are my unit-tests passing when run independently, but fail when running multiple tests? When I execute a single unit test, my tests will successfully mock and return the expected results. However, when I run all unit-tests my previously passing test will fail. One Test Run shouldDoThisAgain() - Pass Multiple Test Runs shouldDoThis() - Pass shouldDoThisAgain() - Fail shouldDoThisAgainAgain() - Fail My tests: @PrepareForTest({OtherMethods.class}) @PowerMockIgnore("javax.management.*")