Mockito错误-这个模拟Android的互动为零

时间:2019-07-29 07:55:49

标签: java android mockito android-testing testcase

我正在想要但未被调用。第checkFingerPrintWhenTouchIdEnabled()行中的verify(fingerPrintHelper, times(1)).initializeFingerPrint(any());方法的模拟错误错误为零     。     甚至我都嘲笑了对象,并在调试initializeFingerPrint(..)函数时调用了该函数。

下面是我要测试的功能,

@RequiresApi(Build.VERSION_CODES.M)
public void checkFingerPrint() {
    if (fingerPrintHelper.isDeviceReadyForFingerPrint()) {
        boolean isFingerPrintEnable = sharedPreference.getBoolean(SpKeys.KEY_FINGER_PRINT, false);
        if (isFingerPrintEnable) {
            fingerPrintHelper.initializeFingerPrint(this);
        }
    } else {
        sharedPreference.putBoolean(SpKeys.KEY_FINGER_PRINT, false).commit();
    }
}

LoginActvity.java

public class LoginActivity extends AppCompatActivity {
public FingerPrintHelper fingerPrintHelper;
ActivityLoginBinding binding;
private LoginViewModel loginViewModel;
private SharedPreferenceManager sharedPreferenceManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    sharedPreferenceManager = new SharedPreferenceManager(getApplicationContext(), SpKeys.MY_SP);
    fingerPrintHelper = new FingerPrintHelper(this);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
    loginViewModel = ViewModelProviders.of(this).get(LoginViewModel.class);
    binding.setViewModel(loginViewModel);
    binding.setLifecycleOwner(this);

    checkFingerPrint();
}


@RequiresApi(Build.VERSION_CODES.M)
public void checkFingerPrint() {
    if (fingerPrintHelper.isDeviceReadyForFingerPrint()) {
        boolean isFingerPrintEnable = sharedPreferenceManager.getBoolean(SpKeys.KEY_FINGER_PRINT, false);
        if (isFingerPrintEnable) {
            fingerPrintHelper.initializeFingerPrint(this);
        }
    } else {
        sharedPreferenceManager.setBoolean(SpKeys.KEY_FINGER_PRINT, false);
    }
}
}

我正在为此功能编写正负测试用例,而checkFingerPrintWhenTouchIdDisabled()测试可以正常工作,但是checkFingerPrintWhenTouchIdEnabled()测试功能却出现错误 请参考下面的测试课程

LoginActivityTest.java

public class LoginActivityTest {

LoginActivity loginActivity;
@Mock
FingerPrintHelper fingerPrintHelper;
@Rule
public ActivityTestRule<LoginActivity> loginActivityRule = new ActivityTestRule<>(
        LoginActivity.class);
Context context;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    loginActivity = loginActivityRule.getActivity();
    context = getInstrumentation().getTargetContext();
}

@Test
public void checkFingerPrintWhenTouchIdDisabled() {

    SharedPreferences sharedPreferences = context.getSharedPreferences(SpKeys.MY_SP, Context.MODE_PRIVATE);
    when(fingerPrintHelper.isDeviceReadyForFingerPrint()).thenReturn(false);
    loginActivity.checkFingerPrint();
    Assert.assertFalse(sharedPreferences.getBoolean(SpKeys.KEY_FINGER_PRINT, false));
    verify(fingerPrintHelper, never()).initializeFingerPrint(any());

}
@Test
public void checkFingerPrintWhenTouchIdEnabled() {

    SharedPreferences sharedPreferences = context.getSharedPreferences(SpKeys.MY_SP, Context.MODE_PRIVATE);
    SharedPreferences.Editor preferencesEditor = sharedPreferences.edit();
    when(fingerPrintHelper.isDeviceReadyForFingerPrint()).thenReturn(true);
    preferencesEditor.putBoolean(SpKeys.KEY_FINGER_PRINT, true).commit();

    loginActivity.checkFingerPrint();

    /* Below verification for `initializeFingerPrint()` is throwing error as,
    Actually, there were zero interactions with this mock. error,
    Even I have mock the object & while debugging the method is getting invoked also.
    If I debug my code it calls this function but in test cases it shows above error
    */
    verify(fingerPrintHelper, times(1)).initializeFingerPrint(any());
}
}

那为什么我的测试用例会出现零交互错误?可能是问题所在,我们将提供任何帮助。

谢谢。

2 个答案:

答案 0 :(得分:1)

您不会在测试用例的任何地方注入模拟。我假设在构造器/工厂中创建了一个普通实例。

使用SUT的二传手或让Mockito为您注入:

@InjectMocks
LoginActivity loginActivity;

仅使用@Mock是不够的。

答案 1 :(得分:1)

尝试以下操作手动设置模拟(不使用注释):

@Before
public void setUp() {
    loginActivity = loginActivityRule.getActivity();
    loginActivity.fingerPrintHelper = Mockito.mock(FingerPrintHelper.class);
    // ...
}

如果loginAcitivy可以在之前成功创建,那么您现在就不会遇到问题。
而且fingerPrintHelper似乎仍然是public,因此设置起来很容易。
但是如果您想正确地进行设置,可以只提供一个设置器。


或者如果您想保留注释以创建fingerPrintHelper

@Mock
FingerPrintHelper fingerPrintHelper;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    loginActivity = loginActivityRule.getActivity();
    loginActivity.fingerPrintHelper = fingerPrintHelper;
    // ...
}

  

我仍然想知道保持loginActivity.fingerPrintHelper = fingerPrintHelper行的原因。

模拟不会神奇地将自身附加到任何其他对象上。

@InjectMocks会为您做到这一点,但是Mockito似乎无法独自处理LoginActivity的创建。

因此,您唯一可以做的就是手动将模拟传递给被测对象。