Skip to content

Class that stubs out android.util.Log for unit testing

Notifications You must be signed in to change notification settings

BehindTheMath/TestWithLogStub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Test With Log Stub

This is a class that stubs out android.util.Log for unit testing, using PowerMock and Mockito. All Log.* methods are redirected to System.out.println(), so the output shows in the JUnit console. This allows easy unit testing of code with calls to Log.*, without requiring extensive modification to implement dependency injection, while still showing the output in the console.

Usage

  1. Copy TestWithLogStub.java to your project, and rename the package name to match your package.
  2. Make sure you have the following lines in the dependencies block in your module-level build.gradle:
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.powermock:powermock-api-mockito:1.6.6'
    testCompile 'org.powermock:powermock-module-junit4:1.6.6'
  1. Subclass the TestWithLogStub class, and add your own methods. Call Log.* as usual, and the output will be redirected to the console.

Example:

package io.behindthemath.test;

import android.util.Log;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TestClass extends TestWithLogStub {
    @Test
    public void test() {
        Log.i("TestClass", "This is a test");

        assertEquals(true, true);
    }
}

Output: I/TestClass: This is a test

License

Copyright 2017 Behind The Math

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http:https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Class that stubs out android.util.Log for unit testing

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages