2014-03-28 30 views
0

在我的網絡系統中,我有一個AppConfig類這樣春季看點在測試環境中不運行

@Configuration 
@ComponentScan(basePackages = "com.mypackage") 
@EnableWebMvc 
@EnableAspectJAutoProxy(proxyTargetClass = true) 
public class AppConfig { 

    @Bean 
    public UrlBasedViewResolver setupViewResolver() { 
     UrlBasedViewResolver resolver = new UrlBasedViewResolver(); 
     resolver.setPrefix("/WEB-INF/pages/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(JstlView.class); 
     return resolver; 
    } 
} 

我也是爲了檢查驗證時,用戶觸發的請求創建Aspect

@Component 
@Aspect 
public class AuthenticationAspect { 
    @Before(value = "@within(com.mypackage.logic.aspects.SessionLookUp) || @annotation(com.mypackage.logic.aspects.SessionLookUp)") 
    public void before(JoinPoint joinPoint) throws FailAuthenticationException { 
     LogFactory.getLog(AuthenticationAspect.class).info("monitor.before, class: " + joinPoint.getSignature().getDeclaringType().getSimpleName() + ", method: " + joinPoint.getSignature().getName()); 

     ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); 
     HttpSession session = attr.getRequest().getSession(true); 

     String username  = (String) session.getAttribute("username"); 
     String role   = (String) session.getAttribute("role"); 
     Boolean isLogined = (Boolean) session.getAttribute("isLogined"); 

     if (
       session  == null || 
       username == null || username.isEmpty() || 
       role  == null || role.isEmpty()  || 
       isLogined == null 
     ) { 
      throw new FailAuthenticationException("You need to login first"); 
     } 

    } 

    @After(value = "@within(com.mypackage.logic.aspects.SessionLookUp) || @annotation(com.mypackage.logic.aspects.SessionLookUp)") 
    public void after(JoinPoint joinPoint) throws Throwable { 
     LogFactory.getLog(AuthenticationAspect.class).info("monitor.after, class: " + joinPoint.getSignature().getDeclaringType().getSimpleName() + ", method: " + joinPoint.getSignature().getName()); 
    } 
} 

SessionLookup接口

@Component 
@Target(value = { ElementType.METHOD, ElementType.TYPE }) 
@Retention(value = RetentionPolicy.RUNTIME) 
public @interface SessionLookUp {} 

這是我的控制器

@Controller 
public class ApplicationController { 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    @ResponseBody 
    @SessionLookUp 
    public String sayHello() { 
      return "Hello"; 
    } 
} 

現在,當我在瀏覽器上運行,我就得到了與該消息的例外「你需要先登錄」,但使用集成測試時,測試將通過Aspect一語不發

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@Transactional 
@ContextConfiguration(classes = { 
    ApplicationController.class, 
    AuthenticationAspect.class, 
    DatabaseConfig.class 
}) 
@TestExecutionListeners({ 
    DependencyInjectionTestExecutionListener.class, 
    DirtiesContextTestExecutionListener.class, 
    TransactionalTestExecutionListener.class, 
}) 
public class ApplicationControllerTest { 

    private MockMvc mockMvc; 

    @Autowired 
    private WebApplicationContext webApplicationContext; 

    @Before 
    public void setUp() {      
     mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 
    } 

    @Test 
    public void testAuthentication() throws Exception { 

     mockMvc.perform(
      get("/") 
     ) 
     .andExpect(
      status().isOk() 
     ); 
    } 
} 

我怎樣才能解決這個問題呢?

+0

當你得到「你需要先登錄」,瀏覽器的http狀態是什麼? – gerrytan

+2

「@ContextConfiguration」中的「AppConfig」在哪裏? –

+0

@SotiriosDelimanolis將您的評論作爲答案。我會調高1000倍 –

回答

1

@ContextConfiguration(classes = { 
    ApplicationController.class, 
    AuthenticationAspect.class, 
    DatabaseConfig.class 
}) 

你似乎缺少您的AppConfig類聲明的看點配置您的@ContextConfiguration聲明。

請注意,您應該刪除ApplicationControllerAuthenticationAspect以及那些將被包含(和管理)AppConfig