Robotium 使用 robotium 执行测试用例时提示 'java.lang.ClassNotFoundException'

yunbin_7 · 2014年12月22日 · 最后由 sky.qian 回复于 2016年10月24日 · 1376 次阅读

大家好,我在创建测试工程后,编写一个测试用例,已经导入 robotium-solo-5.2.1.jar 包,且代码中使用的包名和类名均正确,在执行其中的一个 testCase 时,提示找不到类的异常,如下所示:
Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'‘;
我使用了很多网络上的操作,比如确定启动类 “LAUNCHER_ACTIVITY_FULL_CLASSNAME” 是否正确,导入 junit4 等,
但是问题还是没有办法得到解决,因为之前在执行一样的代码都是可以正常运行的,不会出现这个提示。
想问一下,是否有对设置场景的遗漏导致的呢?大家有没有遇到类似的错误提示呢?谢谢!
代码如下:
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.*;

public class CaiYinLibraryTest extends ActivityInstrumentationTestCase2 {

private Solo solo;
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="xxxxx";
private static String packageName="xxxxxxxx";
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings({ "unchecked", "deprecation" })
public CaiYinLibraryTest() throws ClassNotFoundException{
super(packageName,Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME));
}
@Override
@Before
public void setUp() throws Exception {
solo=new Solo(getInstrumentation());
getActivity();
}

@Override
@After
public void tearDown() throws Exception {
try {
solo.finishOpenedActivities();// 解决执行多个用例时出现卡住的问题
} catch (Throwable e) {
e.printStackTrace();
}
getActivity().finish();
super.tearDown();
}

@Test
public void testDetail() {
solo.sleep(2000);
solo.getCurrentActivity();

RelativeLayout parentView = (RelativeLayout) solo.getView("pic_lick_num_id").getParent();
LinearLayout linearlayout=(LinearLayout) parentView.findViewById(1);
linearlayout.getChildAt(0);
solo.clickOnView(solo.getView("gallery_flower_btn"));

}
}

共收到 9 条回复 时间 点赞

robotium.jar change to top,另外你问题描述应该有上下文。这样描述大家看不懂的

#1 楼 @monkey 嗯,好的,我注意描述一下哈~对了,你说的 robotium.jar change to top,是不是在工程的 build path 里面的 libraries 和 order and export,把 robotium.jart top 到最前面?我刚才试过了下,还是不行。

#2 楼 @yunbin_7 那就是可以详细描述下就是你项目结构怎么样的,然后直接上代码。大家看下吧

#3 楼 @monkey 嗯,好的,我的代码已经有加上去了。其实,我感觉应该是哪里设置有问题,代码是没有问题的哦

#4 楼 @yunbin_7 一。。你是 eclipse 启动这样还是 adb shell 启动这样?

#5 楼 @monkey eclipse 上启动的,执行 run as --〉android juint test 运行的

请问楼主,这个问题最后是怎么解决的呢?

我也遇到过这个 error。
我的原因是把待测应用的 Main activity 给写错了。改正确就 Test run finished 了。

是因为 robotium 不支持 junit4 吧

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册