gpt4 book ai didi

android - findFragmentByTag 的单元测试条件

转载 作者:IT老高 更新时间:2023-10-28 13:37:47 25 4
gpt4 key购买 nike

Android Studio 3.2 Canary 2
Build #AI-173.4591728, built on February 8, 2018
JRE: 1.8.0_152-release-1024-b01 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.14.18-300.fc27.x86_64

我有以下 Activity ,我想对 fragment 已附加并应返回非空值的情况进行单元测试。并且应该属于else条件:

这是我的 Activity :

public class BillingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.billing_container);

if(getSupportFragmentManager().findFragmentByTag(BillingView.TAG) == null) {
final FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();

fragmentTransaction.add(
R.id.billing_view_container,
BillingView.newInstance(),
BillingView.TAG);

fragmentTransaction.commit();
}
else {
Timber.d("Fragment already attached");
}
}
}

测试类是这样的:我可以编写附加 fragment 的测试,但我想测试 fragment 已经附加并且应该返回非空的条件。

class BillingActivityTest: BaseRobolectricTestRunner() {
private lateinit var billingActivity: BillingActivity

@Before
fun setup() {
billingActivity = Robolectric.buildActivity(BillingActivity::class.java)
.create()
.start()
.get()
}

@Test
fun testBillingActivityIsNotNullValue() {
assertThat(billingActivity, `is`(notNullValue()))
}

@Test
fun testBillingFragmentHasStarted() {
val actualFragment = billingActivity
.supportFragmentManager
.findFragmentByTag(BillingView.TAG)

assertThat(actualFragment.tag, `is`(BillingView.TAG))
}

@Test
fun testBillingFragmentAlreadyAttached() {
/* how to test */
}
}

感谢您的任何建议,

最佳答案

方法you've chosen (使用 recreate() )只要它复制您请求的行为就可以工作。我相信这是目前最短的方法。

尽管如此,我不能认为它是一个纯粹的单元测试,因为实际上你在作弊,因为显然你对 Activity 的 recreate() Action 不感兴趣(不妨来有副作用)。

如果您想执行纯单元测试,那么这里就是执行此操作的方法。声明 Activity 如下:


class MainActivity : AppCompatActivity() {

internal var fragmentManagerRetriever = FragmentManagerRetriever()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val fragmentManager = fragmentManagerRetriever.getFragmentManager(this)
if (fragmentManager.findFragmentByTag("TAG") == null) {
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.add(SomeFragment(), "TAG")
fragmentTransaction.commit()
}
}

}

FragmentManagerRetriever 就这么简单:


class FragmentManagerRetriever {

public FragmentManager getFragmentManager(FragmentActivity activity) {
return activity.getSupportFragmentManager();
}

}

那么你有一个接缝来执行纯单元测试:


@Test
fun nullCase() {
val activityController = Robolectric.buildActivity(MainActivity::class.java)
val activity = activityController.get()

val fragmentManagerRetriever = mock(FragmentManagerRetriever::class.java)
val fragmentManager = mock(FragmentManager::class.java)
val fragmentTransaction = mock(FragmentTransaction::class.java)
activity.fragmentManagerRetriever = fragmentManagerRetriever

`when`(fragmentManagerRetriever.getFragmentManager(activity)).thenReturn(fragmentManager)
`when`(fragmentManager.findFragmentByTag("TAG")).thenReturn(null)
`when`(fragmentManager.beginTransaction()).thenReturn(fragmentTransaction)

activityController.create()

val inOrder = Mockito.inOrder(fragmentTransaction)
inOrder.verify(fragmentTransaction).add(ArgumentMatchers.any(SomeFragment::class.java), ArgumentMatchers.eq("TAG"))
inOrder.verify(fragmentTransaction).commit()
}

以及对非空情况的测试:


@Test
fun nonNullCase() {
val activityController = Robolectric.buildActivity(MainActivity::class.java)
val activity = activityController.get()

val fragmentManagerRetriever = mock(FragmentManagerRetriever::class.java)
val fragmentManager = mock(FragmentManager::class.java)
val fragmentTransaction = mock(FragmentTransaction::class.java)
activity.fragmentManagerRetriever = fragmentManagerRetriever

`when`(fragmentManagerRetriever.getFragmentManager(activity)).thenReturn(fragmentManager)
`when`(fragmentManager.findFragmentByTag("TAG")).thenReturn(mock(Fragment::class.java))

activityController.create()

verifyZeroInteractions(fragmentTransaction)
}

关于android - findFragmentByTag 的单元测试条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com