Posts

Android RecyclerView Example

Image
Recently for my app, I needed to provide a scrollable list of clickable items. After doing a little research I found that the RecyclerView is the recommended choice since it recycles view items that are not in the user's focus. Here is a sample app that I created to demonstrate how to implement such a list. Here is what the app looks like. Now to get into the details of how to implement this list. First, let's take a look at how the RecyclerView is setup in a layout XML file. Note: I referenced the Google example, Creating Lists and Cards , which is why the code looks very similar. activity_main.xml 1| <?xml version="1.0" encoding="utf-8"?> 2| <LinearLayout> 3| xmlns:android="http://schemas.android.com/apk/res/android" 4| xmlns:tools="http://schemas.android.com/tools" 5| android:id="@+id/activity_main" 6| android:orientation="vertical" 7| android:layout_width=&quo

Unit Testing a Singleton Using PowerMock

I came across an interesting problem this week at work. I had to make some changes to a Singleton class in Java, and had some issues when trying to unit test the changes. When JUnit runs each test method with the @Test annotation, a new object of the test class is instantiated, this ensures that there are no side effects between test cases. However, the Singleton class that is under test returns a reference to it's object upon each invocation of it static factory method. This can be a problem for your test cases because they are referencing the same object under test, opposed to a new one, which can cause side effects from previous tests. Luckily you can get around this issue using PowerMock. The code snippets below show how to do this yourself... Code Example SingletonTest.java 1| package test; 2| 3| import static org.junit.Assert.fail; 4| import main.Singleton; 5| 6| import org.junit.Before; 7| import org.junit.Test; 8| import org.junit.runn

Verifying Static Method Calls

This section explains how to verify mocked static method calls, and why it's important to include them within unit tests. Code Example There may be cases where some code you want to test interacts with an external system, such as a database for example. You wouldn't want to execute the SQL statements within a unit test, especially if the query takes some time. However, we may want to ensure that the mocked DAO is invoked. This can easily be handled with Mockito, but what if the SQL statement is within a static method? That's where verify comes in handy with PowerMock. Below is an example of such a scenario. UnderTest.java 1| package main; 2| 3| public class UnderTest { 4| public void foo() { 5| DAO.executeSQL(); 6| } 7| } Method under test foo() that invokes the SQL statement. DAO.java 1| package main; 2| 3| public class DAO { 4| public static void executeSQL() { 5| System.out.println("Exec

PowerMock Static Spy

This post shows how to partially mock a class's static methods. Code Example See PowerMock Setup page to learn how to get started with PowerMock. UnderTest.java 1| package main; 2| 3| public class UnderTest { 4| public void foo() { 5| MockClass.foo(); 6| System.out.println("UnderTest!"); 7| MockClass.foo2(); 8| } 9| } MockClass.java 1 | package main; 2 | 4 | public class MockClass { 5 | public static void foo() { 6 | System.out.println("Mocked Method!"); 7 | } 9 | 10| public static void foo2() { 11| System.out.println("foo2!"); 12| } 13| } StaticSpyTest.java 1 | package test; 2 | import main.*; 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.powermock.api.mockito.PowerMockito; 6 | import org.powermock.core.classloader.annotations.PrepareForTest; 7 | import org.p

PowerMock Static Mocking

Here is how to mock static method calls within unit tests using PowerMock. Code Example See PowerMock Setup page to learn how to get started with PowerMock. UnderTest.java 1| package main; 2| 3| public class UnderTest { 4| public void foo() { 5| MockClass.foo(); 6| System.out.println("Unmocked Method!"); 7| } 8| } MockClass.java 1| package main; 2| 3| public class MockClass { 4| public static void foo() { 5| System.out.println("Mocked Method!"); 6| } 7| } StaticMockTest.java 1 | package test; 2 | 3 | import main.*; 4 | import org.junit.Test; 5 | import org.junit.runner.RunWith; 6 | import org.powermock.api.mockito.PowerMockito; 7 | import org.powermock.core.classloader.annotations.PrepareForTest; 8 | import org.powermock.modules.junit4.PowerMockRunner; 9 | 10| @PrepareForTest(MockClass.class) 11| @RunWith(PowerMockRunner.class) 1

Mocking Super Class Method Invocations with PowerMock

Mocking Superclass Method Invocations with PowerMock Code Example See the setup page to learn more about setting up PowerMock. There may be situations when you are forced to call a super class method when overriding. For instance, in Android development, one must constantly interact with the life cycle call back methods. However, when running unit tests on these methods, exceptions are thrown due to Android system dependency interactions in super class method calls (unit tests are run locally on the developer's machine). Therefore, mocking the super class method invocations are crucial towards running successful unit tests. SuperClass.java 1| package main; 2| public class SuperClass { 3| public void foo() { 4| System.out.println("Super Class!"); 5| } 6| } ChildClass.java 1| package main; 2| public class ChildClass extends SuperClass { 3| @Override 4| public void foo() { 5| super.foo()