SharedReference.edit()在JUnit Testing+Mockito中给出了NullPointerE



计算时间的Junit测试类:

{
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代码函数:

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;
}

我已经使用SessionManager类来维护所有SharedPref值。

施工单位:

public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}

editor=pref.edit((;

当我运行UnitTest类时,此行给出NullPointerException。

Getter&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();
}

我对此不确定。但是您错过了注入Mock Annotation Variables的一行代码,这可能是由于发生了异常。

你需要包括在@Before方法中的代码:

MockitoAnnotations.intMocks(this(

也许这会对你有所帮助。

参考:https://gist.github.com/marcouberti/500621c4646f5fdb8d69721d5186cac3

CalculateTimeAgo_Unit_Test.class

package io.intelehealth.client;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import com.parse.Parse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import app.intelehealth.client.activities.homeActivity.HomeActivity;
import app.intelehealth.client.app.IntelehealthApplication;
import app.intelehealth.client.utilities.SessionManager;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

/**
* Created by Prajwal Waingankar
* on 13-Jul-20.
* Github: prajwalmw
*/
@RunWith(MockitoJUnitRunner.class)
public class CalculateTimeAgo_UnitTest {
private HomeActivity homeActivity;
private SessionManager sessionManager;
private SharedPreferences sharedPreferences;
private Context context;
private SharedPreferences.Editor editor;
private SimpleDateFormat simpleDateFormat;
private ParsePosition parsePosition;
private Locale locale;
CalculateTimeAgo_UnitTest cal;
private int PRIVATE_MODE = 0;
private static final String PREF_NAME = "Intelehealth";

@Before
public void Setup()
{
context = mock(Context.class);
simpleDateFormat = mock(SimpleDateFormat.class);
parsePosition = mock(ParsePosition.class);
sharedPreferences = mock(SharedPreferences.class);
editor = mock(SharedPreferences.Editor.class);
when(context.getSharedPreferences(PREF_NAME, PRIVATE_MODE)).thenReturn(sharedPreferences);
when(sharedPreferences.edit()).thenReturn(editor);
sessionManager = new SessionManager(context);
homeActivity = new HomeActivity(); //It is a java class so no mocking of this class.
}
@Test
public void test_calculateTimeAgo()
{
//        when(simpleDateFormat.parse(anyString(), any(ParsePosition.class)).setTime()).thenReturn();
//        when(simpleDateFormat.parse(anyString(), any(ParsePosition.class)).getTime()).thenReturn(4544544L);
when(sessionManager.getLastSyncDateTime()).thenReturn("13-07-2020 20:22");
String result_time = homeActivity.CalculateAgoTime(sessionManager);
assertFalse(result_time.isEmpty());
}
}

我的SharedPreferences问题已经解决。我想用mock(Android_Dependency.class(格式,而不是基于mock的流的注释。

在setup((中,我创建了以下格式的模型:

  • 上下文

  • 共享参考

  • 编辑器

  • context.getsharedpreferences

  • sharedpreferences.edit((

  • 会话管理器实例创建

  • homeactivity实例创建。

当我debugged单元测试时,我意识到了我的错误,并逐行遍历每个代码以找到Null异常。

我注意到空异常位于:

context.getSharedPreferences((

然后,我意识到单元测试按照代码编写的方式进行。按照单元测试类代码中的编写方式逐行创建mock。

确保根据他们的执行血统创建mock。

相关内容

  • 没有找到相关文章

最新更新