是否可以使用 Butterknife 为测试类注入视图?视图被注入到由我的 MainActivity 类创建和提交的片段中。
这是我的测试类的代码:
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mMainActivity;
private Button learnButton;
private Button teachButton;
@SuppressWarnings( "deprecation" )
public MainActivityTest() {
super("com.example.application.app", MainActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
mMainActivity = getActivity();
learnButton = (Button) mMainActivity.findViewById(R.id.buttonLearn);
teachButton = (Button) mMainActivity.findViewById(R.id.buttonTeach);
}
但是我使用 Butterknife 在我的片段中注入视图:
public class ChooseActionFragment extends Fragment {
@InjectView(R.id.buttonTeach) Button buttonTeach;
@InjectView(R.id.buttonLearn) Button buttonLearn;
public ChooseActionFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ButterKnife.inject(this, rootView);
return view;
}
我想知道如何在我的测试中使用 Butterknife 来减少我的样板视图代码,就像我在生产代码中所做的那样。