当我尝试运行SignUpFragmentTest类时,我收到错误“没有注册仪器!必须在注册检测下运行”。我认为当我使用@Rule时会引发误解。
import android.support.test.runner.AndroidJUnit4;
import android.support.v4.app.Fragment;
import android.widget.FrameLayout;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.rule.ActivityTestRule;
import static junit.framework.Assert.assertNotNull;
@RunWith(AndroidJUnit4.class)
public class SignUpFragmentTest {
@Rule public final ActivityTestRule<LoginActivity> main = new ActivityTestRule<>(LoginActivity.class);
private LoginActivity mActivity = null;
@Before
public void setUp() throws Exception {
mActivity =main.getActivity();
}
@Test
public void testLaunchSingUpScreen(){
FrameLayout frameLayout = mActivity.findViewById(R.id.fragment_container);
assertNotNull(frameLayout);
Fragment fragment = new SignUpFragment();
mActivity.getSupportFragmentManager().beginTransaction().add(frameLayout.getId(),fragment).commitAllowingStateLoss();
}
@After
public void tearDown() throws Exception {
mActivity= null;
}
}
我添加依赖项:
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
和
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
到defaultConfig
对我有用的是完全改变所有库
androidX
至
com.android.support
build.gradle中的库
请注意:您可能需要重新导入库以测试您的课程,例如:
import androidx.test.runner.AndroidJUnitRunner;
至
import android.support.test.runner.AndroidJUnitRunner;
还要确保在build.gradle中的每个添加或删除库之后始终清理并重建项目。