powermock

How to use PowerMock with Arquillian?

狂风中的少年 提交于 2019-12-23 12:35:07
问题 I tried to use PowerMockRule in a JUnit test that uses arquillian but I get java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalStateException: PowerMockRule can only be used with the system classloader but was loaded by ModuleClassLoader for Module I want to test something like this: @RunWith(Arquillian.class) @PrepareForTest(WARRRAworkffsTest.class) public class WARRRAworkffsTest { @Rule public PowerMockRule rule = new PowerMockRule(); @Deployment(testable=true) public static

Powermock/mockito does not throw exception when told to

我怕爱的太早我们不能终老 提交于 2019-12-23 10:29:10
问题 I would have assumed that the following test should pass, but the exception is never thrown. Any clues ? @RunWith(PowerMockRunner.class) @PrepareForTest(StaticService.class) public class TestStuff { @Test(expected = IllegalArgumentException.class) public void testMockStatic() throws Exception { mockStatic(StaticService.class); doThrow(new IllegalArgumentException("Mockerror")).when(StaticService.say("hello")); verifyStatic(); StaticService.say("hello"); } } 回答1: It's because you are using the

Mocking a class object using Mockito and PowerMockito

ε祈祈猫儿з 提交于 2019-12-23 08:53:15
问题 Is it possible to mock a class object using Mockito and/or PowerMockito? Something like: Class<Runnable> mockRunnableClass = mock(Class<Runnable>.class); 回答1: An alternative to mocking Class might be to use a Factory instead. I know you are concerned about refactoring, but this could be done without changing the public API of the class. You haven't provided much code to understand the class you are trying to test, but here's an example of refactoring without changing the API. It's a trivial

Mockito mocked object is assigned a null value from method calls inside a method

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 06:25:06
问题 I have a class that I need to test with mockito. Below is the class and the Mockito Test. dbBuilder.parse(file) always returns null because it calls several classes which, in turn, call several methods in a jar file. Even though I mocked all of them it always returns null. I couldn't track where the null value is coming from. I tried to suppress the methods but still no use. Since this method call returns null, the doc value is null. So the doc calls the getElementsByTagName method and

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"))

unit testing static methods using powermock

ε祈祈猫儿з 提交于 2019-12-23 04:52:45
问题 I want to write unit test case for some of the static methods in my project, Snippet of my class code, Class Util{ public static String getVariableValue(String name) { if(isWindows()){ return some string... } else{ return some other string... } } public static boolean isWindows(){ if(os is windows) return true; else return false; } } Basically, i want to write unit test case for getVariableValue() when isWindows() returns 'false'. How do i write this using powermock? 回答1: // This is the way

PowerMock in Scala: java.lang.NullPointerException when getPackage

怎甘沉沦 提交于 2019-12-23 04:41:49
问题 This code just getPackage and getName of a class (not use any mock techniques yet), but it failed. Anyone see this problem before? Code: import mai.MyScala1 import org.junit.Test import org.junit.runner.RunWith import org.powermock.modules.junit4.PowerMockRunner import org.scalatest.junit.JUnitSuite @RunWith(classOf[PowerMockRunner]) class MyTest extends JUnitSuite { @Test def test1() { classOf[MyScala1].getPackage // this one returns null classOf[MyScala1].getPackage.getName // raise java

Powermock not able to mock static methods when using scala, junit

ⅰ亾dé卋堺 提交于 2019-12-22 18:16:27
问题 I have used Powermock previously with java and junit. I have been successfully been able to mock static methods like in the following example: @PrepareForTest({ TimeHelper.class, MainApp.class }) @RunWith(PowerMockRunner.class) public class TestSuite { @Before public void setUp() throws IOException { PowerMockito.mockStatic(TimeHelper.class); Mockito.doReturn("2015-01-01 00:00:00").when(TimeHelper.getUnixTime()); } } However, the same doesn't seem to work when translated to scala. Note that

Can't suppress DriverManager's static initializer block

会有一股神秘感。 提交于 2019-12-22 16:40:44
问题 I have a unit test that attempts to create a SQLException to simulate a database error. In SQLException 's constructor, there is a call to DriverManager , which has a static initialization block. I figured that I could suppress the static block with this type of setup: @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"java.sql.DriverManager"}) public class StaticTest { @Test public void testStaticSuppress() throws Exception { SQLException ex = new SQLException(); expect(...)

Can't suppress DriverManager's static initializer block

僤鯓⒐⒋嵵緔 提交于 2019-12-22 16:40:22
问题 I have a unit test that attempts to create a SQLException to simulate a database error. In SQLException 's constructor, there is a call to DriverManager , which has a static initialization block. I figured that I could suppress the static block with this type of setup: @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor({"java.sql.DriverManager"}) public class StaticTest { @Test public void testStaticSuppress() throws Exception { SQLException ex = new SQLException(); expect(...)