Trying to mock new Object in test with gradle and testng groups

80 views
Skip to first unread message

Mathieu Denis

unread,
Jan 31, 2016, 2:29:12 AM1/31/16
to PowerMock
Hi,

I am using gradle to build my application and I need to run only one group when I build. To do this, there is the following statements in build.gradle :

test {
    useTestNG
() {
        includeGroups
'unit'
   
}
}

Also, for one of the tests, I need to mock the creation of an object, so I used Powermock for that. If there is no constraint on the group (if I comment out the "includeGroups 'unit' "), it works fine, but when I do try with the 'unit' group, I get the following stack trace :
java.lang.ExceptionInInitializerError
    at org
.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
    at org
.mockito.internal.debugging.LocationImpl.<init>(LocationImpl.java:19)
    at org
.mockito.internal.matchers.LocalizedMatcher.<init>(LocalizedMatcher.java:24)
    at org
.mockito.internal.progress.ArgumentMatcherStorageImpl.reportMatcher(ArgumentMatcherStorageImpl.java:32)
    at org
.mockito.Matchers.reportMatcher(Matchers.java:878)
    at org
.mockito.Matchers.any(Matchers.java:291)
    at org
.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withAnyArguments(DefaultConstructorExpectationSetup.java:69)
    at
ClassTestedTest.myTest(ClassTestedTest.java:22)
    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:497)
    at org
.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org
.testng.internal.Invoker.invokeMethod(Invoker.java:645)
    at org
.testng.internal.Invoker.invokeTestMethod(Invoker.java:822)
    at org
.testng.internal.Invoker.invokeTestMethods(Invoker.java:1130)
    at org
.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org
.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org
.testng.TestRunner.privateRun(TestRunner.java:782)
    at org
.testng.TestRunner.run(TestRunner.java:632)
    at org
.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org
.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org
.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org
.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org
.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org
.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org
.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org
.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org
.testng.TestNG.run(TestNG.java:1064)
    at org
.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
    at org
.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
    at org
.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
Caused by: java.lang.NullPointerException
    at org
.mockito.internal.exceptions.stacktrace.StackTraceFilter.filter(StackTraceFilter.java:36)
    at org
.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.filter(ConditionalStackTraceFilter.java:23)
    at org
.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:42)
    at org
.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:30)
    at org
.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
    at org
.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)
    at org
.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
    at org
.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:12)
    at org
.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:11)
   
... 32 more


The class I want to test is the following :
public class ClassTested {
 
public boolean doSomething() throws Exception {
   
ClassInstantiated obj = new ClassInstantiated("someRandomString");
   
try {
      obj
.process();
   
} catch (Exception e) {
     
return false;
   
}
   
return true;
 
}
}

My testClass :
@PrepareForTest(ClassTested.class)
@Test(groups = "unit")
public class ClassTestedTest extends PowerMockTestCase {
 
@InjectMocks
 
private ClassTested classUnderTest;

 
@Mock
 
private ClassInstantiated mockNewInstance;

 
@Test
 
public void myTest() throws Exception {
   
PowerMockito.whenNew(ClassInstantiated.class).withAnyArguments().thenReturn(mockNewInstance);
    doThrow
(new Exception()).when(mockNewInstance).process();

   
boolean valueReturned = classUnderTest.doSomething();

   
Assert.assertFalse(valueReturned);
 
}
}

I use Java 8, gradle 2.4 along as the following dependencies :
    testCompile("org.testng:testng:6.9.6")
    testCompile
('org.powermock:powermock-api-mockito:1.6.4')
    testCompile
('org.powermock:powermock-module-testng:1.6.4')


Mathieu Denis

unread,
Feb 1, 2016, 3:10:53 PM2/1/16
to PowerMock
I have tried from Eclipse and this seems to be related to TestNG group and not a specific issue from gradle TestNG plugin.

From Eclipse, if I run only the TestNG group "unit", it shows the same exception as the one that I get when I build with gradle. It looks like Powermock doesn't support correctly the group feature of testNG.
Reply all
Reply to author
Forward
0 new messages