Skip to content Skip to sidebar Skip to footer

Mocking Class Properties Java/Android

I have a class written by somebody which exposes it properties as public without getters/setters. Now I want to mock this class using Android Mocking framework. I dont want to modi

Solution 1:

Create a mock object for the class and assign a value to the properties variable.

Mock mock = Mock.create(ClassName.class);
mock.property = value;

Solution 2:

Why do you want to mock this class at all? If the only behavior is trivial (i.e. fields), then you can use the class's fields just as they are.

E.g., if setting this up as a stub, do this in your test setup:

DependedOnClass dependedOnObject = new DependedOnClass();
dependedOnObject.name = "Scott";
objectUnderTest.dependedOnObject = dependedOnObject;    // inject the depended on object

or if setting this up as a mock, then do this in your assertions:

assertEquals("Scott", dependedOnObject.name);

Post a Comment for "Mocking Class Properties Java/Android"