瀏覽代碼

[공통][Common] Logg util 클래스 및 테스트 추가
- Log.d() 등 static method 를 테스트 하기 위해서 PowerMockito 추가

hyodong.min 7 年之前
父節點
當前提交
d7442580d6

+ 5 - 1
app/build.gradle

@@ -96,9 +96,13 @@ dependencies {
     androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
     androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
     // Mockito
-    testImplementation "org.mockito:mockito-core:2.21.0"
+//    testImplementation "org.mockito:mockito-core:2.21.0"
     // Robolectric
     testImplementation 'org.robolectric:robolectric:3.8'
     testImplementation "org.robolectric:shadows-support-v4:3.3.2"
     testImplementation "org.robolectric:multidex:3.4.2"
+    // PowerMock
+    testImplementation 'org.powermock:powermock-core:1.7.4'
+    testImplementation "org.powermock:powermock-module-junit4:1.7.4"
+    testImplementation "org.powermock:powermock-api-mockito2:1.7.4"
 }

+ 33 - 0
app/src/main/java/kr/co/zumo/app/lifeplus/util/Logg.java

@@ -0,0 +1,33 @@
+package kr.co.zumo.app.lifeplus.util;
+
+import android.util.Log;
+
+public final class Logg {
+  public static void v(String tag, String msg) {
+    Log.v(tag, msg);
+  }
+
+  public static void d(String tag, String msg) {
+    Log.d(tag, msg);
+  }
+
+  public static void i(String tag, String msg) {
+    Log.i(tag, msg);
+  }
+
+  public static void w(String tag, String msg) {
+    Log.w(tag, msg);
+  }
+
+  public static void e(String tag, String msg) {
+    Log.e(tag, msg);
+  }
+
+  public static void e(Exception e) {
+    e.printStackTrace();
+  }
+
+  public static void e(Throwable e) {
+    e.printStackTrace();
+  }
+}

+ 161 - 0
app/src/test/java/kr/co/zumo/app/lifeplus/util/LoggTest.java

@@ -0,0 +1,161 @@
+package kr.co.zumo.app.lifeplus.util;
+
+import android.util.Log;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.powermock.api.mockito.PowerMockito.when;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({Log.class})
+public class LoggTest {
+
+  @Before
+  public void setUp() throws Exception {
+    PowerMockito.mockStatic(Log.class);
+  }
+
+  @Test
+  public void v() {
+    System.out.println("Running test-------" + "v");
+
+
+    // Log warnings to the console
+    when(Log.v(anyString(), anyString())).thenAnswer(new Answer<Void>()      {
+      @Override
+      public Void answer(InvocationOnMock invocation) throws Throwable {
+        Object[] args = invocation.getArguments();
+        if (args.length > 1) { //cause I'm paranoid
+          System.out.println(invocation.getMethod().getName() + " - Tag: " + args[0] + ", Msg: " + args[1]);
+
+          assertEquals("tag", args[0]);
+          assertEquals("msg", args[1]);
+        }
+        return null;
+      }
+    });
+
+    Logg.v("tag", "msg");
+  }
+
+  @Test
+  public void d() {
+    System.out.println("Running test-------" + "d");
+
+
+    // Log warnings to the console
+    when(Log.d(anyString(), anyString())).thenAnswer(new Answer<Void>()      {
+      @Override
+      public Void answer(InvocationOnMock invocation) throws Throwable {
+        Object[] args = invocation.getArguments();
+        if (args.length > 1) { //cause I'm paranoid
+          System.out.println(invocation.getMethod().getName() + " - Tag: " + args[0] + ", Msg: " + args[1]);
+
+          assertEquals("tag", args[0]);
+          assertEquals("msg", args[1]);
+        }
+        return null;
+      }
+    });
+
+    Logg.d("tag", "msg");
+  }
+
+  @Test
+  public void i() {
+    System.out.println("Running test-------" + "i");
+
+
+    // Log warnings to the console
+    when(Log.i(anyString(), anyString())).thenAnswer(new Answer<Void>()      {
+      @Override
+      public Void answer(InvocationOnMock invocation) throws Throwable {
+        Object[] args = invocation.getArguments();
+        if (args.length > 1) { //cause I'm paranoid
+          System.out.println(invocation.getMethod().getName() + " - Tag: " + args[0] + ", Msg: " + args[1]);
+
+          assertEquals("tag", args[0]);
+          assertEquals("msg", args[1]);
+        }
+        return null;
+      }
+    });
+
+    Logg.i("tag", "msg");
+  }
+
+  @Test
+  public void w() {
+    System.out.println("Running test-------" + "w");
+
+
+    // Log warnings to the console
+    when(Log.w(anyString(), anyString())).thenAnswer(new Answer<Void>()      {
+      @Override
+      public Void answer(InvocationOnMock invocation) throws Throwable {
+        Object[] args = invocation.getArguments();
+        if (args.length > 1) { //cause I'm paranoid
+          System.out.println(invocation.getMethod().getName() + " - Tag: " + args[0] + ", Msg: " + args[1]);
+
+          assertEquals("tag", args[0]);
+          assertEquals("msg", args[1]);
+        }
+        return null;
+      }
+    });
+
+    Logg.w("tag", "msg");
+  }
+
+  @Test
+  public void e() {
+    System.out.println("Running test-------" + "e");
+
+
+    // Log warnings to the console
+    when(Log.e(anyString(), anyString())).thenAnswer(new Answer<Void>()      {
+      @Override
+      public Void answer(InvocationOnMock invocation) throws Throwable {
+        Object[] args = invocation.getArguments();
+        if (args.length > 1) { //cause I'm paranoid
+          System.out.println(invocation.getMethod().getName() + " - Tag: " + args[0] + ", Msg: " + args[1]);
+
+          assertEquals("tag", args[0]);
+          assertEquals("msg", args[1]);
+        }
+        return null;
+      }
+    });
+
+    Logg.e("tag", "msg");
+  }
+
+  @Test
+  public void e1() {
+    Exception e = mock(Exception.class);
+
+    Logg.e(e);
+
+    verify(e).printStackTrace();
+  }
+
+  @Test
+  public void e2() {
+    Throwable e = mock(Throwable.class);
+
+    Logg.e(e);
+
+    verify(e).printStackTrace();
+  }
+}