Final boolean is changing value - android

I have a class variable that is final and is set in the constructor. When I check the value of it in the constructor for the abstract class and the subclass it's the correct value. But when I check it later in a method it's always false. Here's my code.
abstract class AbstractArticleObject extends StructureObject {
final boolean firstArticle;
AbstractArticleObject(Element element, boolean firstArticle) {
super(element);
this.firstArticle = firstArticle;
...
}
}
class ArticleObject extends AbstractArticleObject {
ArticleObject(Element element, boolean firstArticle) {
super(element, firstArticle);
// In this method, firstArticle is whatever was passed in, which is sometimes true.
Log.v(title, String.format(String.valueOf(this.firstArticle));
}
#Override
StructureState getState() {
// In this method, firstArticle is always false.
Log.v(title, String.format(String.valueOf(firstArticle));
if (...) {
...
} else if (...) {
if (firstArticle) {
return StructureState.CAN_OPEN;
} else {
...
}
}
return StructureState.NOT_SET;
}
}
If I'm setting the value in the constructor, and the value is final, why is it returning false even when it was set to true?

Where is getState() called from?
It is possible for final variables to "change" if you access them before they're ever initialized. Consider the following tiny program:
public class Test {
private final boolean value;
public Test() {
doSomething();
this.value = true;
doSomething();
}
private void doSomething() {
System.out.println(value);
}
public static void main(String[] args) {
new Test();
}
}
The output of this program will be
false
true
So, if your getState() method is called from e.g. the constructor of StructureObject, then it will be called before the AbstractArticleObject constructor initializes firstArticle, and it will be false.

Related

Crash appears on a member variable that is null despite Gson converter that never returns null: impossible case

In my Activity, I have a Training object member initialized during onCreate(). All the members of this object are set.
private Training mTraining; is a class member
public class Training extends BaseModel {
...
#SerializedName("state")
public TrainingState state;
....
public TrainingPreview() {
}
This object is got from server (JSON), and I had a converter on this state to ensure this enum can't be null (I use GSON engine):
public class TrainingStateConverter extends EnumConverter<TrainingState> {
public static final Type TYPE = new TypeToken<TrainingState>() {}.getType();
#Override
protected TrainingState deserialize(String value) {
return TrainingState.fromString(value);
}
#Override
protected TrainingState getUnknownValue() {
return TrainingState.UNKNOWN;
}
}
During the setup, I've created the exercise list with the listener to show a specific exercise:
private void refreshExercisesList() {
final Runnable showTrainingParts = new Runnable() {
public void run() {
int nbItems = mCardExercises.setExercises(mTraining.training, mTraining.state,
new FlatCardTrainingProfilePartExercisesView.OnClickExerciseListener() {
#Override
public void showPart(String trainingPartId, int index) {
onClickOnExercisesList(trainingPartId, index);
}
});
}
};
}
...
}
My onClickOnExercisesList() method:
private void onClickOnExercisesList(String trainingPartId, int index) {
...
switch (mTraining.state) {
...
This Activity code works perfectly since couple of months, but yesterday there was a NullPointerException on switch (mTraining.state) :
int com.xxx.model.training.TrainingState.ordinal()' on a null object reference
com.xxx.ui.training.TrainingActivity.onClickOnExercisesList
How is possible guys?
Thank you very much for your help!
This would occur if state did not appear in the JSON.
The TypeConverter is only used if there is a value in the JSON to convert. If the value isn't present, then there's nothing to convert, so the value is whatever the default is, which is null, because you didn't set it:
#SerializedName("state")
public TrainingState state;
To fix the issue, initialize the variable to a default value:
#SerializedName("state")
public TrainingState state = TrainingState.UNKNOWN;

Mockito does not return mockValue when mockito class passed as a dependency

It is a bit tough the explain the situation actually. I mock a class and pass it to another class's constructor. Then, I create the first class and call the first class's method under the second class's method, it returns null. Please check below:
class A {
public String getName() {
return "Something";
}
}
class B {
private A a;
public B(A insA) {
this.a = insA;
}
public String createName() {
return a.getName(); // when this is called, returns null.
}
}
class TestB {
public testBSomething() {
A mockA = mock(A.class);
when(mockA.getName()).thenReturn("Somevalue");
B insB = new B(mockA);
assertEqual("SomeValue", insB.createName()); // insB.createName() = null which should return "Somevalue"
}
}
I also tried doReturn, thenAnswer and spy variations, but no luck. Probably, I miss something, but I could not find. If anyone has any idea, I will really appreciate it. Thank you.
Here,
#RunWith(MockitoJUnitRunner.class)
public class TestB {
#Mock
A mockA;
#InjectMocks
B insB;
#Before
public void setup() {
insB = new B(mockA);
}
#Test
public void testBSomething() {
when(mockA.getName()).thenReturn("SomeValue");
Assert.assertEquals("SomeValue", insB.createName());
}
}

What is Design Pattern where we can pass the string name to a method and that method chooses which further method to call

I recently got following example where we are passing the action name to the method as string and then the method decides the function that needs to be called.
is this a good way of solving problem or is there some better way as well
public static final String ACTION_CHARGING_REMINDER = "charging-reminder";
public static void executeTask(Context context, String action) {
if (ACTION_INCREMENT_WATER_COUNT.equals(action)) {
incrementWaterCount(context);
} else if (ACTION_DISMISS_NOTIFICATION.equals(action)) {
NotificationUtils.clearAllNotifications(context);
} else if(ACTION_CHARGING_REMINDER.equals(action)){
issueChargeReminder(context);
}
}
I'd do something like this. This can be extended as much as you want, and obviously just an example:
static abstract class ActionHandler {
private String action;
public ActionHandler(String action) {
this.action = action;
}
public boolean canHandleAction(String input) {
return this.action.equals(input);
}
public abstract void handleAction();
}
static class OneActionHandler extends ActionHandler {
public OneActionHandler(String action) {
super(action);
}
#Override
public void handleAction() {
//...
}
}
static class TwoActionHandler extends ActionHandler {
public TwoActionHandler(String action) {
super(action);
}
#Override
public void handleAction() {
//...
}
}
static class Test {
private ActionHandler[] handlers;
public Test() {
handlers = new ActionHandler[]{new OneActionHandler("action1"), new TwoActionHandler("action2")};
}
public void handleAction(String action) {
for(ActionHandler i : handlers) {
if(i.canHandleAction(action)) {
i.handleAction();
break;
}
}
}
}
This sounds a lot like the react/redux, action/reduction pattern.
Reducers specify how the application's state changes in response to
actions sent to the store. Remember that actions only describe what
happened, but don't describe how the application's state changes.

Mocked method doesn't return expected value

I want to test a method of my class using Mockito.
public class SplashPresenter {
public volatile State mField1 = State.DEFAULT;
public volatile State mField2 = State.DEFAULT;
boolean stateFlagsAreAllCompleted(#NonNull final ISplashView view) {
if (mField1 == State.COMPLETE //
&& mField2 == State.COMPLETE) {
// Check Forced Update
final int updateCheckResult = checkForcedUpdate(); // <===
if (updateCheckResult == MyConstants.PRODUCTION_UPDATE_AVAILABLE) {
view.displayForcedUpdateAlert(false);
return true;
}
if (updateCheckResult == MyConstants.BETA_UPDATE_AVAILABLE) {
view.displayForcedUpdateAlert(true);
return true;
}
view.restartLoader();
// Move to the home screen
return true;
}
return false;
}
int checkForcedUpdate() {
...... // my codes
}
}
and this is my test class:
public class SplashPresenterTest_ForStateFlags {
private Context mContext;
private ISplashView mView;
#Before
public void setUp() throws Exception {
mContext = Mockito.mock(Context.class);
mView = Mockito.mock(ISplashView.class);
}
#Test
public void stateFlagsAreAllCompleted() throws Exception {
SplashPresenter presenter = Mockito.mock(SplashPresenter.class);
presenter.mField1 = State.COMPLETE;
presenter.mField2 = State.COMPLETE;
when(presenter.checkForcedUpdate()).thenReturn(1);
boolean actual = presenter.stateFlagsAreAllCompleted(mView);
System.out.println("actual: " + actual + ", " +
presenter.mField1 + ", " +
presenter.checkForcedUpdate());
assertTrue(actual);
}
}
Test failure is what happens at the end. This is the output:
actual: false, COMPLETE, 1
The thing that I don't understand is even I change the stateFlagsAreAllCompleted method to following code then still test fails with above output.
boolean stateFlagsAreAllCompleted(#NonNull final ISplashView view) {
return true;
}
You've not yet mocked the behavior for the method stateFlagsAreAllComplete. You need to do:
when(presenter.stateFlagsAreAllComplete(Matchers.any()).thenReturn(true);
You can fine tune the Matchers.any() argument to the class type you want.
EDIT: I see that you are trying the test the method stateFlagsAreAllComplete. Since you are trying to test the method stateFlagsAreAllComplete of the class SplashPresenter, you cannot do so by mocking the class whose method is under test. You will have to work with an instance of the class. Mocked methods should only be used while testing when they are called in another method under test.
You will have to create an instance of the class you want to test.

Using Espresso idling resource with multiple activities

I have a firstActivity that launches the secondActivity, where in the secondActivity I have a loading Dialog (not AsyncTask), and I need to make Espresso wait until the dialog disappears before it continues with the test.
Where do I have to implement the IdlingResource? How can I make it wait for the dismissDialog() function?
Here is what I've tried to do:
class DocumentLoadingIdlingResource implements IdlingResource {
private ResourceCallback callback;
#Override
public String getName() {
return "Documnet loading idling resource";
}
#Override
public boolean isIdleNow() {
Activity activity;
try {
activity = getCurrentActivity();
} catch (Throwable e) {
return false;
}
if(activity.getClass().getName().equals(EditorActivity.class.getName())
&& activity.loadingDialogShowing() == false) {
return false;
}
return true;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
this.callback = callback;
}
}
Activity getCurrentActivity() throws Throwable {
getInstrumentation().waitForIdleSync();
final Activity[] activity = new Activity[1];
runTestOnUiThread(new Runnable() {
#Override
public void run() {
java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = com.google.common.collect.Iterables.getOnlyElement(activites);
}});
return activity[0];
}
This class is implemented in the test class.
There are a few problems here:
Your isIdleNow() calls getCurrentActivity() which calls waitForIdleSync() and runTestOnUiThread(). isIdleNow Javadoc says: "Espresso will always call this method from the main thread, therefore it should be non-blocking and return immediately." So this won't work as is, but you could call getActivitiesInStage directly from isIdleNow.
Your other issue is that you store the reference to ResourceCallback but never invoke onTransitionToIdle, also you should allow for the possibility of more than one ResourceCallback being registered and call onTransitionToIdle on all of the callbacks.
You can do the following:
Copy/Paste IdlingResource into your app as com.mycompany.IdlingResource.
Then have your Activity implement that interface and make sure to call onTransitionToIdle when the dialog goes away and make sure isIdleNow returns false iff the dialog is showing.
In your test code, write a "IdlingResourceAdapter" that wraps com.mycompany.IdlingResource and turns it into an Espresso IdlingResource and register that with Espresso.
This will be simpler once this issue is implemented: https://code.google.com/p/android-test-kit/issues/detail?id=71
I stumbled upon this question in my search for a similar answer. Using concepts from Stefano Dacchille's article on IdlingResources, I built the following idling resource that waits for a specific Activity to be active before firing. In my case, I know the dialog is showing when a fragment with a specific tag exists. This isn't the same as the OP's test, but the concepts should translate well.
public class BusyWhenFragmentExistsInActivityIdlingResource implements IdlingResource {
private FragmentActivity activity = null;
private final String fragmentTag;
private ResourceCallback resourceCallback;
private boolean wasIdleLastTime = true; // Start off as idle
private final String name;
// Need this strong reference because ActivityLifecycleMonitorRegistry won't hold one
private final ActivityLifecycleCallback activityLifecycleCallback;
public BusyWhenFragmentExistsInActivityIdlingResource(
final Class<? extends FragmentActivity> clazz,
final String fragmentTag
){
name = BusyWhenFragmentExistsInActivityIdlingResource.class.getSimpleName()+" "+clazz.getSimpleName();
this.fragmentTag = fragmentTag;
activityLifecycleCallback = new ActivityLifecycleCallback() {
#Override
public void onActivityLifecycleChanged(Activity activity, Stage stage) {
if (!FragmentActivity.class.isAssignableFrom(activity.getClass())) {
return;
}
FragmentActivity fragmentActivity = (FragmentActivity) activity;
if (!clazz.isAssignableFrom(fragmentActivity.getClass())) {
return;
}
switch (stage){
case RESUMED:
BusyWhenFragmentExistsInActivityIdlingResource.this.activity = fragmentActivity;
break;
case STOPPED:
BusyWhenFragmentExistsInActivityIdlingResource.this.activity = null;
break;
}
}
};
ActivityLifecycleMonitorRegistry.getInstance()
.addLifecycleCallback(activityLifecycleCallback);
}
#Override
public String getName() {
return name;
}
#Override
public boolean isIdleNow() {
if (activity==null) {
return wasIdleLastTime = true;
}
boolean isIdleThisTime = activity
.getSupportFragmentManager()
.findFragmentByTag(fragmentTag)==null;
if (!wasIdleLastTime && isIdleThisTime && resourceCallback!=null){
resourceCallback.onTransitionToIdle();
}
return wasIdleLastTime = isIdleThisTime;
}
#Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.resourceCallback = resourceCallback;
}
}
To use it, add something similar to this to your test:
#Before
public void setUp() throws Exception {
registerIdlingResources(new BusyWhenFragmentExistsInActivityIdlingResource(
SomeOtherActivity.class,
BaseActivity.LOADING_DIALOG
));
}

Categories

Resources