代碼如下:
public class UserTest { private static UserServiceImpl userService ; @BeforeClass public static void iniUserService(){ applicationContext applicationContext = new ClassPathxmlApplicationContext("applicationContext.xml") ; userService = (UserServiceImpl) applicationContext.getBean("userService") ; } @Test public void test() { User user = new User(3,"shpengfish@Gmail.com","ysp","123456","yushjunit 報錯
java.lang.ClassCastException: com.sun.proxy.$Proxy13 cannot be cast to com.ysp.surveypark.service.impl.UserServiceImpl 后來上網搜索了下該問題,是因為 UserServiceImpl 實現了 UserService 接口,而該測試類中 卻是使用 UserServiceImpl 聲明的,這并沒有體現出面向接口編程的思想,而 aop 使用的JDK 代理是基于接口的,因此報錯,將實現類改為接口類型即可。
新聞熱點
疑難解答