1

I am trying to establish basic tests but have been getting. several errors. I've tried following the wikis.

http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/InjectMocks.html

but I can't seem to figure out what is the matter. Essentially I'm not actually invoking the mocked methods and that is causing verification to be an issue.

Wanted but not invoked:
mockListener.onCallBellPressed(<any>);
-> at com.callbell.callbell.presentation.bed.CallBellsFragmentTest.start(CallBellsFragmentTest.java:60)
Actually, there were zero interactions with this mock.

at com.callbell.callbell.presentation.bed.CallBellsFragmentTest.start(CallBellsFragmentTest.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)

CallBellFragmentTest.java

package com.callbell.callbell.presentation.bed;


import android.app.Activity;
import android.widget.RelativeLayout;

import com.callbell.callbell.BuildConfig;
import com.callbell.callbell.R;
import com.callbell.callbell.models.State.MessageReason;
import com.callbell.callbell.util.shell.BlankCallBellsFragmentActivity;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.util.FragmentTestUtil;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.verify;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class CallBellsFragmentTest  {

    @Mock(name = "mockListener")
    CallBellsFragment.onCallBellFragmentInteractionListener mActivityListener;

    @Mock(name="mockActivity")
    BlankCallBellsFragmentActivity activity;

    @InjectMocks
    CallBellsFragment fragment = CallBellsFragment.newInstance(false);

    RelativeLayout mBlanketButton;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test(expected = ClassCastException.class)
    public void start_classexception() {
        FragmentTestUtil.startFragment(fragment, Activity.class);
    }

    @Test
    public void start() throws NullPointerException {
        FragmentTestUtil.startFragment(fragment, activity.getClass());

        mBlanketButton = (RelativeLayout) fragment.getView().findViewById(R.id.call_button_blanket);
        Assert.assertNotNull(mBlanketButton);

        mBlanketButton.performClick();

        verify(mActivityListener).onCallBellPressed(any(MessageReason.class));
    }



}

CallBellFragment.java

package com.callbell.callbell.presentation.bed;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.callbell.callbell.R;
import com.callbell.callbell.models.State.MessageReason;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class CallBellsFragment extends Fragment {
    private static String  ORIENTATION_KEY = "ORIENTATION_KEY";
    private onCallBellFragmentInteractionListener mActivityListener;

    @InjectView(R.id.call_button_layout)
    LinearLayout mCallButtonLayout;

    @InjectView(R.id.call_button_restroom)
    RelativeLayout mButtonRestroom;

    @InjectView(R.id.call_button_food_water)
    RelativeLayout mButtonFoodWater;

    @InjectView(R.id.call_button_blanket)
    RelativeLayout mButtonBlanket;

    @InjectView(R.id.call_button_pain)
    RelativeLayout mButtonPain;

    @InjectView(R.id.call_button_help)
    RelativeLayout mButtonHelp;

    private int mOrientation = -1;

    public static CallBellsFragment newInstance(boolean isSimpleMode) {
        CallBellsFragment fragment = new CallBellsFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(ORIENTATION_KEY, isSimpleMode);
        fragment.setArguments(bundle);

        return fragment;
    }

    public CallBellsFragment() {
        // NOOP: Required empty public constructormCallBellsFragment.toggleMode(LinearLayout.VERTICAL);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        mOrientation = LinearLayout.VERTICAL;
    }

 ...

    public class CallBellListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            MessageReason clickedText;
            RelativeLayout clicked = (RelativeLayout) v;
            switch (clicked.getId()) {
                case R.id.call_button_pain:
                    clickedText = MessageReason.PAIN;
                    break;
                case R.id.call_button_blanket:
                    clickedText = MessageReason.BLANKET;
                    break;
                case R.id.call_button_food_water:
                    clickedText = MessageReason.FOOD;
                    break;
                case R.id.call_button_restroom:
                    clickedText = MessageReason.RESTROOM;
                    break;
                case R.id.call_button_help:
                    clickedText = MessageReason.HELP;
                    break;
                default:
                    clickedText = MessageReason.HELP;
            }
            mActivityListener.onCallBellPressed(clickedText);

        }
    }
}
4
  • I don't see that you call MockitoAnnotations.initMocks(this); and I don't see any test methods and issue could be in test method on in code. Commented Mar 12, 2016 at 18:51
  • MockitoAnnotations.initMocks(this); second code block
    – austin
    Commented Mar 12, 2016 at 20:26
  • Sorry, I was from mobile and didn't scroll code. Hmm... everything looks good. By the way, have you checked in debug what instance is called here: mActivityListener.onCallBellPressed(clickedText); Commented Mar 13, 2016 at 6:05
  • Where do you actually install your CallBellListener, such that mBlanketButton receives its click event? Commented Mar 13, 2016 at 18:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.