使用 Junit 执行代码时无法访问 Xml 文件
Not able to access Xml file when code is executed using Junit
我正在研究 "robotium with junit"。
我遇到过这样的情况,我的代码无法在给定路径上找到 Xml 文件。(前提是指定的路径是正确的)。
如果我 运行 与正常 java 相同的代码(文件读取)那么它工作正常。
但是当我运行使用junit的代码时,代码找不到指定的Xml文件。
这是我的代码:参考public void test_insert();
import java.io.File;
import java.io.IOException;
import com.robotium.solo.Solo;
import com.rohit.databse_crud.DbReader;
import com.rohit.databse_crud.insert_act;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
@SuppressWarnings("unchecked")
public class InitialButtonTest extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.xyz.dat.SplashActivity";
private static Class launcherActivityClass;
static {
try {
launcherActivityClass = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public InitialButtonTest() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo solo;
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void test_insert()
{
String name="C:/Users/Marathe/x.xml";
File xmll=new File(name);
if(xmll.exists())
{
System.out.println("file found");
}
else
{
System.out.println("file not found");
}
}
下面是简单的 Java 代码,对我来说效果很好:
import java.io.File;
public class TestFile {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name="C:/Users/Marathe/x.xml";
File xmll=new File(name);
if(xmll.exists())
{
System.out.println("file found");
}
else
System.out.println("file not found");
}
}
O/P : file found
虽然我在指定路径上有 xml 文件,但我从其他地方收到 "file not found" 消息。
在 junit 中还有其他方法可以访问该文件吗?
请帮我解决这个问题。我哪里出错了?
提前致谢。
我将要在测试中使用的示例数据存储在 res/raw 文件夹中。然后我可以通过以下方式读取测试中的数据:
private String getFileData() throws Exception {
Writer writer = new StringWriter();
InputStream is = null;
try {
is = getInstrumentation().getTargetContext().getResources().openRawResource(R.raw.sample_data);
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
char[] buffer = new char[1024];
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
if (is != null) {
is.close();
}
}
return writer.toString();
}
经过很长一段时间,我终于找到了解决方案并且对我有用:
我做了什么?是的,我喜欢这样:
1) 首先我将我的 .xml 文件放入 src/test/resources
2) 然后我编写了访问 xml 文件的代码:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("test/resources/x.xml"));
使用 Junit 执行后,我可以访问该文件。
我正在研究 "robotium with junit"。 我遇到过这样的情况,我的代码无法在给定路径上找到 Xml 文件。(前提是指定的路径是正确的)。
如果我 运行 与正常 java 相同的代码(文件读取)那么它工作正常。
但是当我运行使用junit的代码时,代码找不到指定的Xml文件。
这是我的代码:参考public void test_insert();
import java.io.File;
import java.io.IOException;
import com.robotium.solo.Solo;
import com.rohit.databse_crud.DbReader;
import com.rohit.databse_crud.insert_act;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
@SuppressWarnings("unchecked")
public class InitialButtonTest extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.xyz.dat.SplashActivity";
private static Class launcherActivityClass;
static {
try {
launcherActivityClass = Class
.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public InitialButtonTest() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo solo;
@Override
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void test_insert()
{
String name="C:/Users/Marathe/x.xml";
File xmll=new File(name);
if(xmll.exists())
{
System.out.println("file found");
}
else
{
System.out.println("file not found");
}
}
下面是简单的 Java 代码,对我来说效果很好:
import java.io.File;
public class TestFile {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name="C:/Users/Marathe/x.xml";
File xmll=new File(name);
if(xmll.exists())
{
System.out.println("file found");
}
else
System.out.println("file not found");
}
}
O/P : file found
虽然我在指定路径上有 xml 文件,但我从其他地方收到 "file not found" 消息。
在 junit 中还有其他方法可以访问该文件吗?
请帮我解决这个问题。我哪里出错了?
提前致谢。
我将要在测试中使用的示例数据存储在 res/raw 文件夹中。然后我可以通过以下方式读取测试中的数据:
private String getFileData() throws Exception {
Writer writer = new StringWriter();
InputStream is = null;
try {
is = getInstrumentation().getTargetContext().getResources().openRawResource(R.raw.sample_data);
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
char[] buffer = new char[1024];
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
if (is != null) {
is.close();
}
}
return writer.toString();
}
经过很长一段时间,我终于找到了解决方案并且对我有用:
我做了什么?是的,我喜欢这样:
1) 首先我将我的 .xml 文件放入 src/test/resources
2) 然后我编写了访问 xml 文件的代码:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("test/resources/x.xml"));
使用 Junit 执行后,我可以访问该文件。