2017-05-13 10 views
0

私は基本的なSpringBootアプリを持っています。 Spring Initializer、組み込みTomcat、Thymeleafテンプレートエンジン、およびパッケージを実行可能なJARファイルとして使用しています。SpringBoot:許可付きWebMvcTest

私はこのテストを持っている:

@RunWith(SpringRunner.class) 
@WebAppConfiguration 
@WebMvcTest 
public class MockMvcTests { 

    // Pull in the application context created by @ContextConfiguration 
    @Autowired 
    private WebApplicationContext wac; 

    private MockMvc mockMvc; 

    @MockBean 
    private I18NService i18NService; 

    @MockBean 
    private EmailService emailService; 

    @MockBean 
    private PasswordResetTokenService passwordResetTokenService; 

    @MockBean 
    private UserService userService; 

    @MockBean 
    private CompanyService companyService; 

    @MockBean 
    private UserSecurityService userSecurityService; 

    @Before 
    public void setup() { 
     // Setup MockMVC to use our Spring Configuration 
     this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 
    } 

    /** 
    * 
    * @throws Exception 
    *    If anything fails. 
    */ 
    @Test 
    public void getDeviceEventsTest() throws Exception { 
     this.mockMvc 
       .perform(get("/deviceevent/list") // 
       .accept(MediaType.parseMediaType("text/html;charset=UTF-8"))) 
       .andExpect(status().isOk()) // 
       .andExpect(model().size(1)); // 
    } 

しかし、私はこの例外に

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#authorization.expression('hasRole(''ROLE_ADMIN'')')" (tdk/common/menu:62) 

を持ってテストを実行するには、許可を回避する方法はありますか?

+0

認証ヘッダーの追加はどうですか?ヘッダー( "認証"、 "あなたの認証タイプのヘッダー値")) '.perform(get("/deviceevent/list " –

答えて

0

hasRole(''ROLE_ADMIN'')の奇妙な表現の最初の可能性のある問題は、ここで二重引用符ではなく二重引用符のように見えますが、おそらくログに奇妙に見えます。あなたがテストモードで春のセキュリティを無効にする必要がテストのために認証メカニズムを回避するとにかく

、いくつかの作業の方法があり、それは、より多くの柔軟性がかかるので、私は通常、コンフィギュレーションおよびプロファイル設定でthis方法を使用here

を説明他の記載された方法。

関連する問題