unit testing - PowerMockito and static methods -
i trying test utility class bunch of static methods. writing test method calls 2 static methods. on first static method call mocking seem work disregards second expectation , calls real method. idea? tried providing second powermock.spy(utils.class) before second expectation no luck. idea how around this?
here sample code.
package com.personal.test; import java.io.ioexception; public class utils { public static string testutilone() { system.out.println("static method one"); return "methodone"; } public static void testutilstwo(string s1, string s2, string s3) throws ioexception { throw new ioexception("throw exception"); } public static void testutilsthree() throws testexception { try { string s = testutilone(); testutilstwo("s1", s, "s3"); } catch (ioexception ex) { throw new testexception("there exception", ex); } } public static class testexception extends exception { public testexception() { super(); } public testexception(string message, exception cause) { super(message, cause); } } } package com.personal.test; import java.io.ioexception; import org.junit.assert; import org.junit.test; import org.junit.runner.runwith; import org.mockito.mockito; import org.powermock.api.mockito.powermockito; import org.powermock.core.classloader.annotations.preparefortest; import org.powermock.modules.junit4.powermockrunner; import com.personal.test.utils.testexception; @runwith(powermockrunner.class) @preparefortest({utils.class}) public class utilstest { @test public void testmethodthreewrapsexception() throws exception { powermockito.spy(utils.class); powermockito.when(utils.class, "testutilone").thenreturn("this coming test"); powermockito.spy(utils.class); powermockito.when(utils.class, "testutilstwo", mockito.anystring(), mockito.anystring(), mockito.anystring()).thenthrow(new ioexception("exception test")); try { utils.testutilsthree(); } catch (testexception ex) { assert.asserttrue(ex.getcause() instanceof ioexception); assert.assertequals("exception test", ex.getmessage()); } } }
just in-case if 1 interested, after spending whole day trying got around problem. first staticmocked utils class (this mock static methods) asked mock call real method testutilsthree call. changes below.
package com.personal.test; import java.io.ioexception; import org.junit.assert; import org.junit.test; import org.junit.runner.runwith; import org.mockito.mockito; import org.powermock.api.mockito.powermockito; import org.powermock.core.classloader.annotations.preparefortest; import org.powermock.modules.junit4.powermockrunner; import com.personal.test.utils.testexception; @runwith(powermockrunner.class) @preparefortest({utils.class}) public class utilstest { @test public void testmethodthreewrapsexception() throws exception { powermockito.mockstatic(utils.class); powermockito.when(utils.class, "testutilone").thenreturn("this coming test"); powermockito.when(utils.class, "testutilstwo", mockito.anystring(), mockito.anystring(), mockito.anystring()).thenthrow(new ioexception("exception test")); powermockito.docallrealmethod().when(utils.class, "testutilsthree"); try { utils.testutilsthree(); } catch (testexception ex) { assert.asserttrue(ex.getcause() instanceof ioexception); assert.assertequals("exception test", ex.getmessage()); } } }
Comments
Post a Comment