java


Mocking a static method in abstract class


I have a class A which calls a static method of an abstract class B which throws some exception. I wanted to test for this exception. I am using junit 4.1, mockito 1.9.5 and powermock 1.6.6. The classes are :
abstract class B {
public static void meth(String str) throws SomeException1, SomeException2,SomeException3 {
//some code
}
}
class A{
public void method() throws SomeException1, SomeException2,SomeException3 {
B.meth1("abc");
}
}
I want to test these exceptions and here is my test class
#RunWith(MockitoJUnitRunner.class)
#PrepareForTest(B.class)
class Test throws Throwable {
public void testException(){
PowerMockito.mockStatic(B.class); //Line 6
when(B.meth(Mockito.any(String.class))).thenThrow(new SomeException1(), new SomeException2(), new SomeException3() );
A obj=new A();
obj.method();
}
}
}
While executing this test case I get an exception
org.powermock.api.mockito.ClassNotPreparedException:
The class B not prepared for test.
To prepare this class, add class to the '#PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or method level.
at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.java:32)
at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.java:38)
at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.java:10)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.createMock(DefaultMockCreator.java:56)
at org.powermock.api.mockito.internal.mockcreation.DefaultMockCreator.mock(DefaultMockCreator.java:46)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.java:71)
at Test.testException(Test.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Any suggestion as to why am I getting this ?
You need this: #RunWith(PowerMockRunner.class)
You can use powermock to mock static method.
Code example
#RunWith(PowerMockRunner.class)
public class TestStaticMethodExample {
private SomeClass c = new SomeClass ("g", "123");
#PrepareForTest({ SomeStatic.class })
#Test
public void stubStaticMethod() {
PowerMockito.mockStatic(SomeStatic.class);
PowerMockito.when(SomeStatic.getSummary()).thenReturn(new
SomeClass("t", 9999));
}
}

Related Links

Try Catch and Negative Inputs
Can I make Java Custom rules analyze a Android Project on SonarQube?
Setting data from Android Volley to variable
Java - get number of days between dates [duplicate]
How do I store static core data of my app?
How do I reopen an account in java, using a constructor with differnt argument lengths?
Adding a class into a frame and fixing button
Receiving Exception in thread “Thread-1” java.lang.NullPointerException for a map object that is not null [duplicate]
Reasons for using a Bag in Java
Moving an object in Java (KeyPressed)
Why cant I draw a rectangle with fillPolygon()
EhCache Spring XML integration namespace
Java android Fragment tabs (changed fragment only when swipe) don't work click on tabs name
Weblogic Server getting SAXParse Exception
Java Sockets. Server-Client communication
Reading REST Request Output using HttpURLConnection

Categories

HOME
java
keycloak
sd-card
rdf
electronics
dxl
icloud
reveal.js
google-apps-marketplace
windows-phone-7
flask-wtforms
decimal
progressive-web-apps
facebook-page
pugjs
reactcsstransitiongroup
firefox-webextensions
amazonsellercentral
reverse-proxy
accessor
core-text
blazemeter
firebase-crash-reporting
selectedindexchanged
internet-explorer-9
entitlements
minitab
file-rename
pdb
catel
fog
mpmediaquery
hockeyapp
splice
android-tabhost
janrain
eigenvalue
executenonquery
babel-core
jquery-validate
diagnostics
nodebb
feeds
ableton-live
libpng
httplistener
probability-density
login-control
player
simplewebrtc
prettytensor
hover-over
system.management
uid
javax.sound.midi
ora-00900
jqgrid-formatter
tarjans-algorithm
deis
ionic
appfabric-cache
sailfish-os
sankey-diagram
unity5.2.3
streambase
system.reflection
gui-test-framework
asp.net-dynamic-data
fpml
internet-connection
terminfo
htmlcleaner
fmod
prettify
tws
lcs
cassini-dev
itmstransporter
simba
nsnetservice
labwindows
tridion-worldserver
onsubmit
jquery-ui-layout
joyent
javax.script
table-footer
inotifycollectionchanged
celltable
datareader
modelstate
perfect-hash
kdbg
procedural-music

Resources

Encrypt Message