jeudi 4 mai 2017

Test a class that calls static wrapper methods

I have a static wrapper class for the android Log. It looks like this (simplified):

// Source: http://ift.tt/2pJfwTo
public class LogUtil {
    private static final int CALLING_METHOD_INDEX;

    static {
        int i = 1;
        for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
            i++;
            if (ste.getClassName().equals(LogUtil.class.getName())) {
                break;
            }
        }
        CALLING_METHOD_INDEX = i;
    }

    public static String getTag() {
        try {
            final StackTraceElement ste = Thread.currentThread().getStackTrace()[CALLING_METHOD_INDEX];
            return "(" + ste.getFileName() + ":" + ste.getLineNumber() + ")";

        } catch (Exception e){
            return "TAG";
        }
    }

    public static void v(String message) {
        Log.v(getTag(), message);
    }
}

It's pretty helpful because you can click the lines in Android Studio logcat.

Problem is, that I'm testing a class that calls this LogUtil's method.

Is there a way to test them with Mockito and not Powermock(ito) if possible? I read that if I have to use Powermockito, I'm doing something wrong (=> not follwing TTD).

Aucun commentaire:

Enregistrer un commentaire