0

I want to test the class below using junit and mockito but no other third party library.

Below is my Logger wrapper class

import android.util.Log

public class Logger{
public static void v(String tag,String logMessage){
        Log.v(tag,logMessage);
}
    public static void v(String tag,String logMessage,Throwable throwable){
        Log.v(tag,logMessage);
    }
}

Below is how I try to unit test the above class

import android.util.Log;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ModelTest {

    @Before
    public void  setup(){
  when(Logger.v(anyString(),anyString())).thenCallRealMethod(Log.v(anyString(),anyString()));
  when(Logger.v(anyString(),anyString()),any(Throwable.class)).thenCallRealMethod(Log.v(anyString(),anyString()));
    }

    @Test
    public void v(){
        Logger.v("Tag.v"," Message from Tag.v");
        Logger.v("Tag.v throw"," Message from Tag.v throw");
    }



}

In the Before, when() method is not accepting anyString() type. Please, any before approach. I will really appreciate

  • I'm not sure why you're wrapping static method with static methods that literally just pass through arguments, but your problem is you're calling `when` on a function, not a mock. Mockito is not designed for mocking static functions. See https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito – dominicoder Apr 10 '23 at 20:34
  • Any other suggestion? let says I don't use Mock. – ulrich Attiogbe Jun 16 '23 at 17:16
  • I, personally, don't understand what you're trying to achieve. Again, wrapping static functions with static functions that just pass through to them is fairly pointless, as is testing a function that has one line with no logic. – dominicoder Jun 18 '23 at 15:18
  • Sorry to come back to this silly question. This method is used to write a log file and it is working fine. If you were ask to test it, would you skip because it has no logic ? if no. share a snap of code that you will try. Thanks – ulrich Attiogbe Jun 19 '23 at 17:59
  • This does not "write a log file", this prints a statement to LogCat. And yes, I would skip it because it has no logic. I would not even write the methods because they serve no purpose. Why do `Logger.v("string")` when I can write `Log.v("string")`? – dominicoder Jun 20 '23 at 19:34

0 Answers0