Junit test class for Calculation the Time:
{
    private HomeActivity homeActivity;
    private SessionManager sessionManager;
    private String result_time;
    @Mock
    Context context;
    @Mock
    SharedPreferences.Editor editor;
    @Mock
    SharedPreferences sharedPreferences;
    @Before
    public void Setup()
    {
         homeActivity = new HomeActivity();
         sessionManager = new SessionManager(context);
        when(context.getSharedPreferences(anyString(), anyInt()))
                .thenReturn(sharedPreferences);
        when(sharedPreferences.edit()).thenReturn(editor);
        when(editor.putString(anyString(), anyString())).thenReturn(editor);
    }
    @Test
    public void test_calculateTimeAgo()
    {
        when(sessionManager.getLastSyncDateTime()).thenReturn("13-07-2020 20:22");
        result_time = homeActivity.CalculateAgoTime();
        assertFalse(result_time.isEmpty());
    }
}
Java Code Function:
   public String CalculateAgoTime() {
        String finalTime = "";
        String syncTime = sessionManager.getLastSyncDateTime();
        SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
        ParsePosition pos = new ParsePosition(0);
        long then = formatter.parse(syncTime, pos).getTime();
        long now = new Date().getTime();
        long seconds = (now - then) / 1000;
        long minutes = seconds / 60;
        long hours = minutes / 60;
        long days = hours / 24;
        String time = "";
        long num = 0;
        if (days > 0) {
            num = days;
            time = days + " " + context.getString(R.string.day);
        } else if (hours > 0) {
            num = hours;
            time = hours + " " + context.getString(R.string.hour);
        } else if (minutes >= 0) {
            num = minutes;
            time = minutes + " " + context.getString(R.string.minute);
        }
//      <For Seconds>
//      else {
//            num = seconds;
//            time = seconds + " second";
//      }
        if (num > 1) {
            time += context.getString(R.string.s);
        }
        finalTime = time + " " + context.getString(R.string.ago);
        sessionManager.setLastTimeAgo(finalTime);
        return finalTime;
    }
I have used SessionManager class for maintaing all the SharedPref values.
Constructor:
 public SessionManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }
editor = pref.edit();
This line gives NullPointerException when I run my UnitTest class.
Getter & Setter for SharedPef:
public String getLastSyncDateTime() {
        return pref.getString(LAST_SYNC_SUCCESS_DATE_TIME, "- - - -");
    }  //getting the sync value  and time and saving in the sharedpref
    public void setLastSyncDateTime(String lastPulledDateTime) {
        editor.putString(LAST_SYNC_SUCCESS_DATE_TIME, lastPulledDateTime);
        editor.commit();
    }
 
    