In my web system, I have an AppConfig
class like this
@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;
}
}
I also create Aspect
class in order to check the authentication when user triggers a request
@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());
}
}
with the SessionLookup
interface
@Component
@Target(value = { ElementType.METHOD, ElementType.TYPE })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface SessionLookUp {}
This is my controller
@Controller
public class ApplicationController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
@SessionLookUp
public String sayHello() {
return "Hello";
}
}
Now, when I run on the browser, I will got the exception with the message "You need to log in first", but when using the integration test, the test will pass the Aspect
without saying anything
@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()
);
}
}
How can I fix this one ?