2013-11-14 70 views
0

我正在爲android編寫單元測試,此測試應該驗證所使用的URL是否有效,我有一個url不變量和一個錯誤的方式進行測試,但測試沒有通過,我使用JUnit「測試未能運行到完成。儀器運行失敗,由於'java.lang.NullPointerException''

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(); 
     } 

    } 
} 

這是我的錯誤:。

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

那麼,logcat說什麼?我確信你知道他們在[這個部分...]中說了什麼...](http://www.quickmeme.com/img/94/94eb289f247f6e711a9975bed6783d1db4a15af4a9f2b7cd8f1a560a3bfb540a.jpg) – keyboardsurfer

回答

0

從這個代碼的唯一的事情(假設所有方法像getTargetContext()做返回的東西)我自己爲什麼在以下行:

task1 = new FeedControle(context, null); 

FeedControle對象初始化爲null而其他時間帶有ref值?

task2 = new FeedControle(context, tela); 

但是我不知道這些類。

相關問題