尝试按照示例https://developer.android.com/guide/navigation/navigation-testing,我有一个预期的错误
org.mockito.exceptions.base.MockitoException:Mockito 无法模拟此类:类 androidx.navigation.NavController。
即使包含所有依赖项,如下所示: https: //developer.android.com/training/testing/set-up-project
@RunWith(AndroidJUnit4::class)
class MyShopFragmentTest {
@Test
fun testMyShopScenario_F001() {
// Create a mock NavController
val mockNavController = mock(NavController::class.java)
// Create a graphical FragmentScenario for the MyShopFragment
val myShopFragmentScenario = launchFragmentInContainer<MyShopFragment>()
// Set the NavController property on the fragment
myShopFragmentScenario.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), mockNavController)
}
// Verify that performing a click prompts the correct Navigation action
onView(ViewMatchers.withId(R.id.search)).perform(ViewActions.click())
verify(mockNavController).navigate(R.id.searchFragment)
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法?
趋势视图模型测试
@RunWith(JUnit4::class)
class TrendingViewModelTest {
private lateinit var trendingRepository: TrendingRepository
private lateinit var trendingViewModel: TrendingViewModel
@get:Rule
val schedulers = RxImmediateSchedulerRule()
@Before
fun setUp() {
trendingRepository = mock(TrendingRepository::class.java)
trendingViewModel = TrendingViewModel(trendingRepository)
}
@Test
fun testWithNetwork() {
trendingViewModel.isConnected = true
trendingViewModel.fetchTrendingRepos()
verify(trendingRepository, times(1)).getTrendingRepos()
}
//...
}
Run Code Online (Sandbox Code Playgroud)
趋势视图模型
fun fetchTrendingRepos() {
if (isConnected) {
loadingProgress.value = true
compositeDisposable.add(
trendingRepository.getTrendingRepos().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ response ->
run {
loadingProgress.value = false
},
{ error ->
loadingProgress.value = false
}
)
)
}
Run Code Online (Sandbox Code Playgroud)
RxImmediateSchedulerRule:
class RxImmediateSchedulerRule : …
Run Code Online (Sandbox Code Playgroud) 我打算用来FragmentScenario
单独测试片段。在此片段中,我正在访问父活动以调用一些方法。为此,我正在使用CommonActivityOperations
接口。我在此应用程序中使用导航架构组件。
interface CommonActivityOperations {
fun closeSearchBar()
fun navigateBackWithResult(resultFor: String, result: Bundle)
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在我的设备上运行测试时,AttractionDestinationFragment
出现以下错误。
java.lang.ClassCastException: androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity cannot be cast to .interfaces.CommonActivityOperations
景点目的地片段
const val DESTINATION_DATA_EXTRA = "destination_data_result"
class AttractionDestinationFragment : BaseFragment(), ItemClickListener, ToolbarSearchChangeListener{
private lateinit var navigationController: NavController
private lateinit var defaultAttractionList: MutableList<AttractionDestinations>
private lateinit var attractionDestinationRecyclerViewAdapter: AttractionDestinationRecyclerViewAdapter
private val viewModel by lazy { getViewModel<AttractionDestinationViewModel>() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_attraction_destination, container, false)
}
override fun onViewCreated(view: View, …
Run Code Online (Sandbox Code Playgroud) 我创建了一个类型的自定义对象,Task
我想将其保存在内部存储中的二进制文件中.这是我创建的类:
public class Task {
private String title;
private int year;
private int month;
private int day;
private int hour;
private int minute;
public Task(String inputTitle, int inputYear, int inputMonth, int inputDay, int inputHour, int inputMinute) {
this.title = inputTitle;
this.year = inputYear;
this.month = inputMonth;
this.day = inputDay;
this.hour = inputHour;
this.minute = inputMinute;
}
public String getTitle() {
return this.title;
}
public int getYear() {
return this.year;
}
public int getMonth() {
return this.month;
}
public …
Run Code Online (Sandbox Code Playgroud)