运行嘲笑测试时无法模仿功能

时间:2020-02-21 13:26:10

标签: mockito mockmvc

我正在使用Mockmvc编写控制器的测试用例

@Mock
private AService aService;

@InjectMocks
private AController aController;



@BeforeEach
    public void init() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders
                .standaloneSetup(aController)
                .setCustomArgumentResolvers(putAuthenticationPrincipal)  // for passing the authentication principal
                .build();
    }

现在当我尝试测试

@Test
    public void testfunction() throws Exception {
        String id = UUID.randomUUID().toString();
  Mockito.when(aService.getAccount(Mockito.anyString())).thenThrow(new Exception("not avalible"));
        mockMvc.perform(MockMvcRequestBuilders.get("/account/{id}", id)
....      
   }

在这种情况下,aService.getAccount()不会被模拟。这就是为什么我没有得到理想的结果。

我不明白为什么在这种情况下无法模拟功能。

1 个答案:

答案 0 :(得分:0)

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="false" android:windowTranslucentNavigation="false" tools:context=".MainActivity"> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swipeRefresh" android:layout_width="0dp" android:layout_height="0dp" android:overScrollMode="never" android:soundEffectsEnabled="true" app:layout_constrainedHeight="true" app:layout_constrainedWidth="true" app:layout_constraintBottom_toTopOf="@id/BottomLayout" app:layout_constraintEnd_toEndOf="@id/BottomLayout" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:scrollbars="none" android:visibility="gone" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <include android:id="@+id/netCon" layout="@layout/network_connection" /> <include android:id="@+id/srvCon" layout="@layout/server_connection" /> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/BottomLayout" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="wrap_content" android:layout_height="50dp" android:animateLayoutChanges="true" android:background="@drawable/border_top_bottom_menu" android:clipToPadding="false" android:duplicateParentState="false" android:fitsSystemWindows="false" android:focusableInTouchMode="false" android:theme="@style/bottom_navigation_menu" android:visibility="visible" app:barrierAllowsGoneWidgets="true" app:itemHorizontalTranslationEnabled="false" app:itemIconSize="22dp" app:itemIconTint="@color/drawer_item" app:itemTextColor="@color/drawer_item" app:labelVisibilityMode="labeled" app:layout_constraintBottom_toTopOf="@id/Notification" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:menu="@menu/bottom_navigation_menu"> </com.google.android.material.bottomnavigation.BottomNavigationView> <LinearLayout android:id="@+id/Notification" android:layout_width="0dp" android:layout_height="0dp" android:background="@color/notification_offline" android:gravity="center_horizontal|center_vertical" android:orientation="vertical" android:weightSum="1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <TextView android:id="@+id/notificationText" android:layout_width="match_parent" android:layout_height="wrap_content" android:fontFamily="@font/roboto" android:textAlignment="center" android:textColor="#FFFFFF" android:textSize="12sp" android:textStyle="normal" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout> <ProgressBar android:id="@+id/progressbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_gravity="center|center_vertical" android:indeterminate="false" android:theme="@style/progressWhite" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> 中,您不应创建控制器的模型。 Spring需要在其上下文中创建一个控制器,并向其中注入协作服务。

要这样做:

  • @WebMvcTest注释测试类
  • 使用@WebMvcTest(YourController.class)而非@MockBean注释协作服务