Java. lang. ClassCastException occurs during Spring aop testing. springexception
The Code is as follows:
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","yushipeng",new Date()) ; System.out.println(user); userService.saveEntity(user) ; }}
Junit Error
Java. lang. ClassCastException: com. sun. proxy. $ Proxy13 cannot be cast to com. ysp. surveypark. service. impl. UserServiceImpl
Later, I searched the internet for this problem because UserServiceImpl implemented the UserService interface, but this test class uses the UserServiceImpl declaration, which does not reflect the idea of programming the interface, the JDK proxy used by aop is based on interfaces. Therefore, if an error is reported, change the implementation class to the interface type.