私は@MockBean
依存関係で、春ブートアプリケーションのHandlerInterceptor
をテストするための右のコンフィギュレーションを見つけようとしているのテスト時(@PostContruct
コントローラコールの後に@Before
コールが来ることを知っている)避けコントローラの初期化、が、全体Beanプールを初期化せずに春のブートHandlerInterceptor
今のところ、私はこの構文になってきた:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class MyHandlerInterceptorTest {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@Autowired
private RequestMappingHandlerMapping handlerMapping;
@MockBean
private ProprieteService proprieteService;
@MockBean
private AuthentificationToken authentificationToken;
@Before
public void initMocks(){
given(proprieteService.methodMock(anyString())).willReturn("foo");
}
@Test
public void testInterceptorOptionRequest() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/some/path");
request.setMethod("OPTIONS");
MockHttpServletResponse response = processPreHandleInterceptors(request);
assertEquals(HttpStatus.OK.value(), response.getStatus());
}
}
しかし、テストが失敗し、は@PostContructコールを持つので、この時点で嘲笑されていないproprieteService
モックからデータを取得しようとします。
私の質問は次のとおりです。Springbootテストローダーがどのように私のコントローラを初期化するのを防ぐことができますか?1:私はテストの必要はありません。
ユニットテストを作成する統合テストではありません。 'HandlerInterceptor'をインスタンス化し、モックを作成して注入します。 –
その場合、インターセプタに '@ autowired'依存関係をモックする方法はありますか?特別なスプリングブートの注釈が必要になります。@ SpringBootTestがその仕事をしていました。 – Aphax