/ / Spring Rest Controller単体テストでのメソッド引数の検証スキップ - spring-mvc、spring-boot、spring-restcontroller

スプリングレストコントローラユニットテストにおけるメソッドの引数検証をスキップする - spring-mvc、spring-boot、spring-restcontroller

私は、コントローラメソッドの引数を検証するためにSpringを設定しました。 MethodValidationPostProcessor アプリケーション構成内のBean、および追加 @validated コントローラ上の注釈。

public Entity getEntity(@MyConstraint @RequestParam  int limit)

MyConstraint 検証はアプリケーションで適用されますが、単体テストを実行しているときは検証は実行されません。

テストクラスは以下のようになります:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class ControllerTest {
@Before
public void setup()
{
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new ControllerExceptionHandler())
.build();
}
@Test
public void testCase() throws Exception
{
mockMvc.perform(get("locale?limit=-1")).andExpect(status().isBadRequest());
}

どのようなアイデアが私のテストで間違っていますか? ありがとう。

回答:

回答№1は0

私は@WebAppConfigurationアノテーションをテストクラスに追加する必要があると思います。