我有多個安全測驗類,我在其中測驗對不同 REST 端點的授權,我想模擬 ApplicationContext 將啟動的所有存盤庫的 bean。我運行了測驗并且一切正常,但我不想在每個安全測驗類中復制我的 @MockBeans。
我已經嘗試創建一個配置類并將 MockBeans 放在那里,但是我收到了 404 錯誤。
當前的測驗類是這樣設定的,它可以作業,但我必須在每個類中復制@MockBeans():
@WebMvcTest(value = [ScriptController, SecurityConfiguration, ScriptPermissionEvaluator])
@ExtendWith(SpringExtension)
@MockBeans([@MockBean(ObjectRepository), @MockBean(ChannelRepository),
@MockBean(ChannelSubscriberRepository)])
@ActiveProfiles("test")
class ScriptControllerSecurityTest extends Specification
我想設定一個這樣的配置類,并在測驗類中使用 @ContextConfiguration(classes = MockRepositoryConfiguration) 在我的測驗類中使用它。
@Configuration
@MockBeans([@MockBean(ObjectRepository), @MockBean(ChannelRepository),
@MockBean(ChannelSubscriberRepository)])
class MockRepositoryConfiguration
{}
uj5u.com熱心網友回復:
嘗試創建自定義元注釋,如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.Type)
@MockBeans([@MockBean(ObjectRepository), @MockBean(ChannelRepository),
@MockBean(ChannelSubscriberRepository)])
public @interface CustomMockBeans {
}
然后用它注釋你的測驗類:
@WebMvcTest(value = [ScriptController, SecurityConfiguration, ScriptPermissionEvaluator])
@ExtendWith(SpringExtension)
@CustomMockBeans
@ActiveProfiles("test")
class ScriptControllerSecurityTest extends Specification
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/354963.html
