我被通缉,但没有被援引。在行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());
}
}


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

提前致谢。

最佳答案

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

@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的创建。

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

09-12 04:23