2016-07-19 5 views
0

私はRestControllerを持っており、Spring MVC Testでテストしようとしています。それはそれで、次のModelAttributeがありますSpring Test - 模擬@ModelAttribute

@ModelAttribute("authUser") 
    public User authUser(@AuthenticationPrincipal SpringAuthUser springAuthUser) throws Exception { 
     User user = ConstantsHome.userprofileMgr.getUserByUserId(springAuthUser.getUsername(), true, true); 
     user.updateRights(null); 
     request.getSession().setAttribute(ConstantsHome.USEROBJECT_KEY, user); 
     return user; 
    } 

私はこのRestControllerに対してテストを実行すると、私はこのauthUserメソッド内NullPointerExceptionを取得しています。

mockedメソッドがこのメソッドの代わりにテストに使用されるように、このメソッドをモックする方法はありますか?私はこれについて他の記事を読んだが、私はテストで "authUser"パラメータを渡すことができると思ったが、それは動作していない。最終的に私は私のauthUserは、以下のテスト構成クラスを経由して仕事を得ることができました...この「AUTHUSERは」NPEをスローしない...ここに私のテストがあることを確認するために

@Test 
    public void testGetAllUsers() throws Exception { 
     String userJson = AvadaObjectMapper.getMapper().writeValueAsString(new User()); 
     System.out.println("userJson=" + userJson); 
     MvcResult result = this.mockMvc.perform(get("/").param("authUser", userJson).accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) 
      .andExpect(status().isOk()) 
      .andExpect(content().contentType("application/json")) 
      .andDo(print()) 
      .andReturn(); 

     String content = result.getResponse().getContentAsString(); 

     assertTrue(content.contains("Hello")); 
    } 
+1

私は 'ConstantsHome.userprofileMgr'があるかわからないが、それはおそらく、代わりにあなたが、その後にモックを注入し、動作をスタブすることができ、クラス内の' Autowired' @フィールドでなければなりません。 –

答えて

0

をしようとしています。 .defaultRequest...行に注意してください。特に、認証ユーザーが確立される場所です。すべてのテストで、以下のクラスを@ContextConfigurationクラスとして使用します。

import com.avada.rest.api.users.UserService; 
import com.avada.rest.api.users.TestUserService; 
import com.avada.rest.security.SpringAuthUser; 

import java.util.HashSet; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.FilterType; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.authority.SimpleGrantedAuthority; 
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; 
import org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 
import org.springframework.web.context.WebApplicationContext; 

@Configuration 
@ComponentScan(excludeFilters={ 
       @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=UserService.class) 
       }) 
public class TestSpringRestConfig { 

    public static final SpringAuthUser AUTH_USER = 
     new SpringAuthUser("test", "test", 
      new HashSet<GrantedAuthority>() {{ 
       add(new SimpleGrantedAuthority("ROLE_ADMIN")); 
      }} 
     ); 

    @Bean 
    public UserService userService() { 
     return new TestUserService(); 
    } 

    @Bean 
    public MockMvc mockMvc(WebApplicationContext wac) { 
      return MockMvcBuilders 
       .webAppContextSetup(wac) 
       .defaultRequest(MockMvcRequestBuilders.get("/").with(SecurityMockMvcRequestPostProcessors.user(AUTH_USER))) 
       .apply(SecurityMockMvcConfigurers.springSecurity()) 
       .build(); 
    } 
}