Unit tests with Powermockrunner showing errors

Asked

Viewed 41 times

0

I have two classes of tests that are run with PowerMockRunner and extending the class JdbcTest.

Below follows their source code:

Test class ScimImportUsersBusinessTest

@RunWith(PowerMockRunner.class)
@PrepareForTest({DaoFactory.class})
public class ScimImportUsersBusinessTest extends JdbcTest { 
    
    @Mock
    private IdGeneratorSequenceDao idGeneratorSequenceDao;      
    
    
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this); 
        PowerMockito.mockStatic(DaoFactory.class);
        PowerMockito.when(DaoFactory.getDao(IdGeneratorSequenceDao.class)).thenReturn(idGeneratorSequenceDao);
        
    }   
    
    @Test
    public void importUsersScimEmptyListTest() throws ScimImportUserException {
        (...)
    }
    
    @Test
    public void importUsersScimWithManagerTest() throws ScimImportUserException, SQLException {
        (...)   
    }
    
    @Test
    public void importUsersScimNewDepartmentAndPositionTest() throws ScimImportUserException, SQLException {
        (...)       
    }
}

Test class ScimImportUserDaoDefaultTest

@RunWith(PowerMockRunner.class)
@PrepareForTest({ DaoFactory.class })
public class ScimImportUserDaoDefaultTest extends JdbcTest {
    
    @Mock
    private IdGeneratorSequenceDao idGeneratorSequenceDao;  

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        PowerMockito.mockStatic(DaoFactory.class);
        PowerMockito.when(DaoFactory.getDao(IdGeneratorSequenceDao.class)).thenReturn(idGeneratorSequenceDao);
    }   

        
    @Test
    public void importUserAdinterfaceValidadeCountRecords() throws ScimImportUserException, SQLException {
        (...)
    }

    @Test
    public void importUserAdinterfaceValidadeFields() throws ScimImportUserException, SQLException {
        (...)
    }
        
    //outros testes
    (...)   

}

If I squeegee separately each of the above classes with the Junit, the tests are run without errors.

Now, when run the two test classes with Junit together (for example, running all test classes of the project in which they are present), the test class ScimImportUserDaoDefaultTest wheel without errors and the test class ScimImportUsersBusinessTest glitch.

Follows below the stack trace error that occurs in the attempt to run the class ScimImportUsersBusinessTest (by running all project tests) :

Running com.domain.authentication.scim.dao.ScimImportUserDaoDefaultTest
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.063 sec
Running com.domain.authentication.scim.ScimImportUsersBusinessTest
Tests run: 4, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 1.135 sec <<< FAILURE!
importUsersScimEmptyListTest(com.domain.authentication.scim.ScimImportUsersBusinessTest)  Time elapsed: 0.234 sec  <<< ERROR!
java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:h2:mem:;MODE=MSSQLServer
        at com.domain.database.mock.H2ConnectionHandler.createConnection(H2ConnectionHandler.java:76)
        at com.domain.database.mock.H2ConnectionHandler.<init>(H2ConnectionHandler.java:23)
        at com.domain.database.mock.JdbcTest.initializeData(JdbcTest.java:18)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:132)
        at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:95)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
        at org.junit.rules.Verifier$1.evaluate(Verifier.java:35)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
        at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
        at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
        at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
        at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
        at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
        at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:h2:mem:;MODE=MSSQLServer
        at java.sql.DriverManager.getConnection(DriverManager.java:689)
        at java.sql.DriverManager.getConnection(DriverManager.java:247)
        at com.domain.database.mock.H2ConnectionHandler.createConnection(H2ConnectionHandler.java:74)
        ... 39 more

All said:

  1. Why does this occur error when I run both test classes together?

  2. How to correct this error?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.