I'm having trouble mocking a static method in a third-party library. I keep receiving a null-pointer exception when running the test, but I'm not sure why that is.
Here is the class and the void method that invokes the static method I'm trying to mock "MRClientFactory.createConsumer(props)":
public class Dmaap {
Properties props = new Properties();
public Dmaap() {
}
public MRConsumerResponse createDmaapConsumer() {
System.out.println("at least made it here");
MRConsumerResponse mrConsumerResponse = null;
try {
MRConsumer mrConsumer = MRClientFactory.createConsumer(props);
System.out.println("made it here.");
mrConsumerResponse = mrConsumer.fetchWithReturnConsumerResponse();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mrConsumerResponse;
}
}
Below is the test that keeps returning a null-pointer exception. The specific line where the null-pointer is being generated is: MRClientFactory.createConsumer(Mockito.any(Properties.class));
#RunWith(PowerMockRunner.class)
#PrepareForTest(fullyQualifiedNames = "com.vismark.PowerMock.*")
public class DmaapTest {
#Test
public void testCreateDmaapConsumer() {
try {
Properties props = new Properties();
PowerMockito.mockStatic(MRClientFactory.class);
PowerMockito.doNothing().when(MRClientFactory.class);
MRClientFactory.createConsumer(Mockito.any(Properties.class));
//MRClientFactory.createConsumer(props);
Dmaap serverMatchCtrl = new Dmaap();
Dmaap serverMatchCtrlSpy = spy(serverMatchCtrl);
serverMatchCtrlSpy.createDmaapConsumer();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Please follow this example carefully: https://github.com/powermock/powermock/wiki/MockStatic
Especially you are missing a
#PrepareForTest(Dmaap.class)
…to denote the class which does the static call.
Related
Below is code for which i'm trying to write text case and added what i did but getting null pointer exp
public boolean doVersionLimitCheck(Long mneId) throws DMMException {
CALogUtil.getInstance().logMethodEntry("doVersionLimitCheck",
ConfigArchiveManagerImpl.class.getName());
boolean status = false;
status = validateArchivedVersions(mneId);
CALogUtil.getInstance().logDebug("Version Roll over status::" + status);
CALogUtil.getInstance().logMethodExit("doVersionLimitCheck",
ConfigArchiveManagerImpl.class.getName());
return status;
}
for this i did like below.
#Test
public void testDoVersionLimitCheck() {
Long mneId=Long.valueOf("123");
ConfigArchiveManagerImpl impl = new ConfigArchiveManagerImpl();
try {
Mockito.doReturn(true).when(Mockito.mock(ConfigArchiveManagerImpl.class)).validateArchivedVersions(Mockito.anyLong());
} catch (DMMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
impl.doVersionLimitCheck(mneId);
} catch (DMMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You need to spy on the SUT in order to test one method and mock the other:
#Test
public void testDoVersionLimitCheck() {
Long mneId=Long.valueOf("123");
ConfigArchiveManagerImpl impl = Mockito.spy(new ConfigArchiveManagerImpl());
try {
Mockito.doReturn(true).when(impl ).validateArchivedVersions(Mockito.anyLong());
} catch (DMMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
How to invoke Array-name from IPSUM.java class to updateArticleView method by passing its name simply?
eg. in highlighted row, i want to achieve this article.setText(Ipsum.LateralPull[0]); dynamically
updateArticleView method
IPSUM.java
A very simple way to do that is to use a HashMap<String, String[]> and you can put each array to the map with its current field name as its key:
map.put("LateralPull", new String[]{"LateralPaull"});
So you can simply call:
map.get(name);
But If you don't want to use HashMap for any reason you can use java reflection. Here is a sample code:
public class Main {
public static void main(String[] args) {
try {
Test test = new Test();
Field field = Test.class.getDeclaredField("list");
String[] list = (String[]) field.get(test);
System.out.println(list[0]);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String[]> map = new HashMap<>();
map.put("hasahn", new String[]{"df"});
}
private static class Test{
String[] list = new String[]{"Item 1"};
}
}
The output is: Item 1.
Goal: Ignore test classes on runtime which have custom annotation set.
What I tried:
public void onStart(ITestContext context) {
if (context instanceof TestRunner) {
Map<Class<?>, ITestClass> notSkippedCl = new HashMap<Class<?>, ITestClass>();
TestRunner tRunner = (TestRunner) context;
Collection<ITestClass> testClasses = tRunner.getTestClasses();
for (Iterator<ITestClass> iterator = testClasses.iterator(); iterator.hasNext();) {
ITestClass rr = iterator.next();
Class<?> realClass = rr.getRealClass();
if (chechAnnotation(realClass))
{
notSkippedCl.put(realClass,rr);
}
}
try {
Field field = TestRunner.class.getDeclaredField("m_classMap");
field.setAccessible(true);
Map<Class<?>, ITestClass> mapClass = (Map<Class<?>, ITestClass>) field.get(tRunner);
mapClass.clear();
mapClass.putAll(notSkippedCl);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
onStart method is called before all test classes in package, So I get TestRunner here, which contains map of all test classes. I iterate throw each one, checking it Annotation and if I find one I add to new map. Then I override map of TestRunner. I was thinking that this will help me ignore classes without annotation, but I was wrong.
Maybe someone knows right solution, to Ignore test classes depending on custom annotation?
(parameter of the method cannot be changed)
P.S. setting #Test(enabled=false) annotation is not a solution in my situation
--EDIT_FAUND_SOLUTION--
I managed to create solution, not sure if there was easier way, but this works:
#Override
public void onStart(ITestContext context) {
if (context instanceof TestRunner) {
Set<ITestNGMethod> methodstodo = new HashSet<ITestNGMethod>();
TestRunner tRunner = (TestRunner) context;
ITestNGMethod[] allTestMethods = tRunner.getAllTestMethods();
SupportedBrowser currentBrowser = HelperMethod.getCurrentBrowser();
for(ITestNGMethod testMethod : allTestMethods)
{
Class<?> realClass = testMethod.getTestClass().getRealClass();
Set<SupportedBrowser> classBrowsers = getBrowsers(realClass);
if (classBrowsers.contains(currentBrowser)) {
methodstodo.add(testMethod);
}
}
try {
Field field = TestRunner.class.getDeclaredField("m_allTestMethods");
field.setAccessible(true);
field.set(tRunner, methodstodo.toArray(new ITestNGMethod[methodstodo.size()]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
I'd recommend to create org.testng.IMethodInterceptor as a Listener. TestNG calls intercept method just before test suite starts. You get list of all methods as a parameter and have to return modified/new/etc. list with methods you want to run. See documentation http://testng.org/doc/documentation-main.html#methodinterceptors for more details and examples.
when I debug the below code, there is an SmbException and goes catch block line sb.append(pLogger.reportError(pStr, e));, but it does not go into the method reportError().
what is the reason behind this. please advise if any changes.
try {
sfos = new SmbFileOutputStream(sFile);
} catch (SmbException e) {
sb.append(pLogger.rError(pathStr, e));
}
below is rError() method
public String rError(String pxString,Exception e){
String errorToMailStr=null;
abcd="Verifying # "+pxString+"::Error ["+e.getMessage()+"]";
logger.debug("Error when verifying # "+pxString+":Error ["+gMsg(e)+"]");
return abcd;
}
at line logger.debug("Issue "+pxString+":Error ["+gMsg(e)+"]");
is going to below method and ends.
public abstract class ReflectiveCallable {
public Object run() throws Throwable {
try {
return runReflectiveCall();
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
}
Based on what you have revealed here, there is a problem in getExceptionMsg()
I'm trying to load the radio version of the Android device using reflection. I need to do this because my SDK supports back to API 7, but Build.RADIO was added in API 8, and Build.getRadioVersion() was added in API 14.
// This line executes fine, but is deprecated in API 14
String radioVersion = Build.RADIO;
// This line executes fine, but is deprecated in API 14
String radioVersion = (String) Build.class.getField("RADIO").get(null);
// This line executes fine.
String radioVersion = Build.getRadioVersion();
// This line throws a MethodNotFoundException.
Method method = Build.class.getMethod("getRadioVersion", String.class);
// The rest of the attempt to call getRadioVersion().
String radioVersion = method.invoke(null).toString();
I'm probably doing something wrong here. Any ideas?
Try this:
try {
Method getRadioVersion = Build.class.getMethod("getRadioVersion");
if (getRadioVersion != null) {
try {
String version = (String) getRadioVersion.invoke(Build.class);
// Add your implementation here
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.wtf(TAG, "getMethod returned null");
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
What Build.getRadioVersion() actually does is return the value of gsm.version.baseband system property. Check Build and TelephonyProperties sources:
static final String PROPERTY_BASEBAND_VERSION = "gsm.version.baseband";
public static String getRadioVersion() {
return SystemProperties.get(TelephonyProperties.PROPERTY_BASEBAND_VERSION, null);
}
According to AndroidXref this property is available even in API 4. Thus you may get it on any version of Android through SystemProperties using the reflection:
public static String getRadioVersion() {
return getSystemProperty("gsm.version.baseband");
}
// reflection helper methods
static String getSystemProperty(String propName) {
Class<?> clsSystemProperties = tryClassForName("android.os.SystemProperties");
Method mtdGet = tryGetMethod(clsSystemProperties, "get", String.class);
return tryInvoke(mtdGet, null, propName);
}
static Class<?> tryClassForName(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}
static Method tryGetMethod(Class<?> cls, String name, Class<?>... parameterTypes) {
try {
return cls.getDeclaredMethod(name, parameterTypes);
} catch (Exception e) {
return null;
}
}
static <T> T tryInvoke(Method m, Object object, Object... args) {
try {
return (T) m.invoke(object, args);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getTargetException());
} catch (Exception e) {
return null;
}
}