I've been tasked to write an unit test for this method below. However, I'm fairly new to the whole mocking and/or using PowerMockito. This is the method:
public class Storage {
public static JSONObject addHeader(JSONObject input) throws JSONException {
try {
JSONObject header = new JSONObject();
header.put("update_date", System.currentTime());
header.put("created_date", System.currentTime());
header.put("type", "storage");
input.put("HEADER", header);
}
catch (JSONException e) {
e.printStackTrace();
}
return input;
}
And this is where I got stuck. In my test method, I have this line of code.
JSONObject testInput = Whitebox.invokeMethod(Storage, "addHeader", JSONMockTest());
//testInput is always return null so this is where I'm stuck at and as not sure what to do after this
I want to verify that the return JSONMockTest would contain those additional key and value after running through the method. It's probably very simple but I'm not sure how to start.
Any assistance is greatly appreciated.
You don't need to use Whitebox.invokeMethod
as long as your method is not private.
From Whitebox.invokeMethod javadoc:
Invoke a static private or inner class method. This may be useful to test private methods.
Just invoke it directly:
JSONObject resultJsonObject = Storage.addHeader(jsonObject);
Now you can assert the values of resultJsonObject
.