すべての統合テストファイルのすべての@BeforeEachで実行したいコードが少しあります。基本的に、追加する必要があるコードは次のとおりです。
@MockBean
RequestInterceptor interceptor; // I NEED THIS
@BeforeEach
public void initTest() throws Exception {
Mockito.when(interceptor.preHandle(any(), any(), any())).thenReturn(true); // AND THIS
}
すべてのファイルでこの部分が重複しないようにする方法はありますか?たぶん、テスト構成ファイルを作成し、テストファイルで注釈を使用できます。私はjavaの春のブーツに非常に新しいので、私はいくつかの助けに感謝します。ありがとう。
BaseTestなどのスーパークラスを作成し、このコードをそこに移動できます。そして、すべてのテストはBaseTestを拡張するだけです。さらに、このクラスのすべての注釈を設定できます。例えば:
@AutoConfigureMockMvc
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
@ExtendWith(MockitoExtension.class)
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@SpringBootTest
public class BaseTest {
@MockBean
RequestInterceptor interceptor; // I NEED THIS
@BeforeEach
public void initTest() throws Exception {
Mockito.when(interceptor.preHandle(any(), any(), any())).thenReturn(true); // AND THIS
}
}
そして、すべてのテスト:
class MeasurementsServiceTest extends BaseTest {
//test methods here
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加