0

I am working on "robotium with junit". I have come across the situation where, my code is not able to find the Xml file on the given path.(Provided that the specified path is correct).

If I run the same code(file reading) with normal java then its working fine.

But when i run the code with junit, code cannot find the specified Xml file.

Here is my code: Refer 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");
    }
}

Here is the simple Java code which works fine for me:

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

Though i am having the xml file with me on the specified path, I am getting "file not found" message from else.

Is there any other way in junit, to access the file ?

Please help me to solve this issue. Where exaclty i am going wrong ?

Thanks in advance.

10
  • A few things to try out: misspelled path, capital letters in path where they shouldn't, or even special characters not escaped (such '/'). Commented Apr 27, 2015 at 1:30
  • something else: try moving the file to the root folder (c:/) and change the path in the unit test accordingly to see if it is read. Commented Apr 27, 2015 at 1:31
  • one more thing I'm thinking of: try to run the lines to read the xml from a brand new unit test that does not extend ActivityInstrumentationTestCase2, just a simple JUnit test, to see what happens if you discard all possible influence from the father test. Commented Apr 27, 2015 at 1:37
  • Thanks germanio, i tried all possible solutions u suggested but i could not able to access the xml file. Actually, i am little bit surprised that the file is accessible from normal java code but when we use junit, file is no more accessible. I also tried google solutions, but i found those are not adequate. So any other solution u could suggest ? Commented Apr 28, 2015 at 4:34
  • 1
    it looks very similar to this question: stackoverflow.com/questions/18676235/… please take a look and see if that helps you Commented Apr 29, 2015 at 21:10

2 Answers 2

1

I store the sample data that I want to use in my tests in the res/raw folder. Then I can read the data in my tests with the following:

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();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks adavis..I have one query.. "How and In which format u save the test data in res/raw (xml or txt) ? "
I store my data as JSON and then parse it with Gson, but you can store it in whatever format works best for you.
1

Finally after long time, i got the solution for this and it worked for me:

What i did ? Yup, i did like this:

1) First I put my .xml file into src/test/resources

2) Then I wrote this code for accessing xml file :

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("test/resources/x.xml"));

And after executing it using Junit, I could access the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.