Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Log.d() print nothing when running Android Local Unit Test?

I'm trying to print something when running Android Local Unit Test, but nothing's happening. What's the matter? How can I fix it?

I consulted some documents on http://developer.android.com, found that Android Local Unit Test just run on my machine's JVM, the android.jar file that is used to run unit tests does not contain any actual code, so Log.d() print nothing. If i wanna print log, how can i do?

Here is my code, FeedbackModelTest.java located in src/test/main directory.

package com.upward.reader.mvp.model;  import android.util.Log;  import com.upward.reader.mvp.bean.FeedbackBean; import com.upward.reader.net.RequestJSONCallback;  import org.junit.Test;  import java.io.IOException; import java.util.HashMap; import java.util.Map;  public class FeedbackModelTest {  @Test public void postFeedback() throws Exception {     final String url = "http://test.guguread.com/interface/app/user/feedback?";     Map<String, String> params = new HashMap<>();     params.put("content", "content");     new FeedbackModel().postFeedback(url, params, new RequestJSONCallback<FeedbackBean>() {          @Override         public void onResponse(FeedbackBean result) throws IOException {             Log.d("TAG", result.toString());         }          @Override         public void onFailure(Exception e) {             e.printStackTrace();         }      }); } 

}

like image 632
Will Tang Avatar asked May 12 '16 14:05

Will Tang


People also ask

Why is my Logcat empty Android studio?

Go to the File option > click on “INVALIDATE CACHES/RESTART” then a dialog box will pop up, Select the “INVALIDATE CACHES/RESTART” button. This will automatically restart and build the index of android studio.

Should you unit test DTO?

@sepehr It's relevant insofar as DTOs are not usually unit-tested because there is no behavior to test. DTOs don't have behavior because behavior isn't serializable, which is specifically the point of DTOs. So it's very common to hear and read that DTOs don't need testing.

What does log d do in Java?

The Log. v() method is used to log verbose messages. The Log. d() method is used to log debug messages.

How do I run unit test on Android?

To run all tests in a class or a specific method, open the test file in the Code Editor and do either of the following: Press the Run test icon in the gutter. Right-click on the test class or method and click Run . Select the test class or method and use shortcut Control+Shift+R .


1 Answers

You should use standard output, System.out.println("Hello StackOverflow") intead of Log.x(). Then you can see the log info in the run tab. enter image description here

2017/12/16 Update: If you can't see the output on the Run tab, go to Android Monitor tab to find the output instead.

like image 99
Jeff T. Avatar answered Sep 18 '22 16:09

Jeff T.