gpt4 book ai didi

java - Spring Aspect 未在测试环境中运行

转载 作者:行者123 更新时间:2023-12-01 13:12:56 29 4
gpt4 key购买 nike

在我的网络系统中,我有一个像这样的 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接口(interface)

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

这是我的 Controller

@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()
);
}
}

我该如何解决这个问题?

最佳答案

您的@ContextConfiguration声明为

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

您似乎缺少声明 Aspect 配置的 AppConfig 类。

请注意,您可能应该删除 ApplicationControllerAuthenticationAspect 以及将由 AppConfig 包含(和管理)的内容。

关于java - Spring Aspect 未在测试环境中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22703947/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com