Ludimila
Ludimila

Reputation: 19

"Test failed to run to completion. Instrumentation run failed due to 'java.lang.NullPointerException''

I'm writing unit tests for android, this test should verify that the url used is valid, I have a url invariant and a wrong way to perform the test, but the test does not pass. I am using JUnit.

public class FeedControleTeste extends  ActivityUnitTestCase<TelaFeed> {

    private static Context context;
    private final String FEED_ADDRESS = "http://noticias.gov.br/noticias/rss?id=AFSZW";
    private FeedControle task1, task2;
    private TelaFeed tela;

    public FeedControleTeste() {
        super(TelaFeed.class);
    }

    public void setUp() throws Exception {
        context = getInstrumentation().getTargetContext();
        tela = getActivity();
    }

    public void tearDown() throws Exception {
    }

    public void testTask() {
        final CountDownLatch signal = new CountDownLatch(1);

        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    task1 = new FeedControle(context, null);
                    task2 = new FeedControle(context, tela);
                    try {
                        task1.execute(FEED_ADDRESS);
                        task2.execute("url_errada");
                    } catch (Exception e) {
                        fail();
                    }
                }
            });

            signal.await(30, TimeUnit.SECONDS);
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

This is my error:

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''. Check device logcat for details"

Upvotes: 0

Views: 4647

Answers (1)

JBA
JBA

Reputation: 2844

From this code the only thing (assumed all the methods like getTargetContext() do return something ) i aks myself why in the following line:

task1 = new FeedControle(context, null);

the FeedControle object is initialized with null while the other time with a ref value?

task2 = new FeedControle(context, tela);

I however dont know those classes.

Upvotes: 1

Related Questions