当我尝试运行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
答案 0 :(得分:0)
对我有用的是将所有库完全从原来改回
androidX
到
com.android.support
build.gradle中的库
请注意:您可能必须重新导入库以测试您的课程,例如
import androidx.test.runner.AndroidJUnitRunner;
到
import android.support.test.runner.AndroidJUnitRunner;
还要确保在每次从build.gradle添加或删除库之后,始终清理并重建项目。