@InjectMocks不起作用

时间:2016-03-12 08:43:48

标签: android testing mockito

我正在尝试建立基本测试,但一直在进行。几个错误。我试过跟随维基。

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

但我似乎无法弄清楚是什么问题。基本上我实际上并没有调用模拟方法,这导致验证成为一个问题。

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);

        }
    }
}

0 个答案:

没有答案