我有一个像这样的抽象AccountRequiredActivity(并且工作正常):
public abstract class AccountRequiredActivity extends LifecycleActivity {
@Inject
ViewModelProvider.Factory viewModelFactory;
private AccountViewModel accountViewModel;
public abstract void doOnCreate(Bundle savedInstanceState);
public abstract void doOnResume();
@Override
protected final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_app);
AndroidInjection.inject(this);
accountViewModel = ViewModelProviders.of(this, viewModelFactory).get(AccountViewModel.class);
if(!accountViewModel.isAuthenticated()) {
redirectToLogin();
} else {
doOnCreate(savedInstanceState);
};
}
@Override
protected void onResume() {
super.onResume();
if(!accountViewModel.isAuthenticated()) {
redirectToLogin();
} else {
doOnResume();
};
}
private void redirectToLogin() {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
测试期间的问题是我没有办法设置viewModelFactory活动.
当一个活动有片段时,我可以做类似的事情:
@Before
public …Run Code Online (Sandbox Code Playgroud) 我想类似的设置UI测试GithubBrowserSample,它看起来像示例项目只有模仿ViewModel的Fragment,但不是一个例子Activity.
这是我的代码,我试图Activity通过嘲笑来测试ViewModel.但是在Activity ViewModel之前没有设置好onCreate().
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
val viewModel = mock(MainViewModel::class.java)
@Rule
@JvmField
val activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java, true, true)
private val liveData = MutableLiveData<Resource<Object>>()
@Before
open fun setUp() {
activityRule.activity.viewModelFactory = createViewModelFor(viewModel)
`when`(viewModel.liveData).thenReturn(liveData)
viewModel.liveData?.observeForever(mock(Observer::class.java) as Observer<Resource<Object>>)
liveData.postValue(Resource.success(Object()))
}
fun <T : ViewModel> createViewModelFor(model: T): ViewModelProvider.Factory =
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(model.javaClass)) {
return model as …Run Code Online (Sandbox Code Playgroud) android kotlin android-espresso android-viewmodel android-architecture-components
与此问题相关:Espresso, Dagger2 set ViemodelProvider.Factory on BaseActivity
在测试期间,我历经千辛万苦才在 Activity 上获取 ViewModelFactory.Provider,以便让 espresso 测试能够针对 Android 架构组件进行工作。我以为事情会很简单,但我想事实并非如此......
@Before
public void init() {
UserFragment fragment = UserFragment.create("foo");
viewModel = mock(UserViewModel.class);
when(viewModel.getUser()).thenReturn(userData);
when(viewModel.getRepositories()).thenReturn(repoListData);
navigationController = mock(NavigationController.class);
fragmentBindingAdapters = mock(FragmentBindingAdapters.class);
fragment.viewModelFactory = ViewModelUtil.createFor(viewModel);
fragment.navigationController = navigationController;
fragment.dataBindingComponent = () -> fragmentBindingAdapters;
activityRule.getActivity().setFragment(fragment);
}
Run Code Online (Sandbox Code Playgroud)
但是,这根本不适用于活动,因为我无法在使用ActivityTestRule.
我使用 Dagger2 遵循与上面示例中使用接口相同的新依赖项注入流程HasActivityInjector。
我将感谢您的帮助!