powermock method invocation exception

46 views
Skip to first unread message

CHINNA CHINNI

unread,
Jan 13, 2019, 4:20:23 PM1/13/19
to PowerMock
public class DBConnection {

    public static Connection getConnection(String dataSource){
        Connection con = null;
        try
        {
            try {               
                ClassPathResource resource = new ClassPathResource("application-config.xml");
                XmlBeanFactory factory = new XmlBeanFactory(resource);
                DataSource ds = null;       
                ds =(DataSource)factory.getBean(dataSource);
                con = ds.getConnection();               
            }catch (SQLException se){
                se.printStackTrace();
                System.out.println("SQL Exception in getConnection "+se);
            }       

public class DeactivateRenewalExpired {





    private static Connection db2Con = null;
    private MailBean mail = new MailBean();


    public static void main(String[] args)throws SQLException {
        System.out.println("dfdfdf");
        try{
            Environment.PROPERTIES_FILE = args [0];
            db2Con = DBConnection.getConnection(Environment.DATASOURCE_DB2);

            DeactivateRenewalExpired renewal = new DeactivateRenewalExpired();      
            renewal.deactivate();
    }

im trying to write junit for main method and init im trying to mock DBConnection using powermock , im getting exception coz getConnection is a static method , the following is junit class

@RunWith(PowerMockRunner.class)
@PrepareForTest( { DeactivateRenewalExpired.class })
public class DeactivateRenewalStaticTest {

    @Mock
    Connection con;

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @PrepareForTest( { DeactivateRenewalExpired.class })
    @Test
    public void stubStaticMethodToGetMiniStatement() {

        // PowerMockito.spy(DeactivateRenewalExpired.class);
        // PowerMockito.doNothing().when(DeactivateRenewalExpired.class);


        String[] data = { "C://data" };
        try {
            PowerMockito.mockStatic(DBConnection.class);
PowerMockito.when(DBConnection.getConnection("")).thenReturn(con);



                //then
                PowerMockito.verifyStatic();
                DBConnection.getConnection("data");
            DeactivateRenewalExpired.main(data);

}

im getting this error when i run the junit

java.lang.NoClassDefFoundError: org.mockito.internal.MockitoInvocationHandler
at java.lang.J9VMInternals.verifyImpl(Native Method)
at java.lang.J9VMInternals.verify(J9VMInternals.java:72)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:134)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:138)
at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:83)
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:49)
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:307)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:297)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
Reply all
Reply to author
Forward
0 new messages