Spring整合junit出现的console和junit界面无提示的异常
/**
* @author:xhz
* @time: 2018年8月15日
*/
package com.xhz.springAOP;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @author xhz
* @scription:增强UserServiceImpl下的方法,即添加事务
*/
public class MyAdvice {
public void before(){
System.out.println("前置通知!");
}
public void afterRurning(){
System.out.println("后置通知(没异常)!");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕通知之前执行!");
Object object=pjp.proceed();
System.out.println("环绕通知之后执行!");
return object;
}
public void afterException(){
System.out.println("啊啊啊啊!有异常啊!");
}
public void after(){
System.out.println("后置通知!");
}
}
/**
* @author:xhz
* @time: 2018年8月15日
*/
package com.xhz.springAOP;
/**
* @author xhz
* @scription:实现用户的增删改查
*/
public interface UserService {
public void addUser();
public void delUser();
public void updUser();
public void queUser();
}
/**
* @author:xhz
* @time: 2018年8月15日
*/
package com.xhz.springAOP;
/**
* @author xhz
* @scription:实现UserService接口
*/
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("增加用户!");
}
@Override
public void delUser() {
System.out.println("增加用户!");
}
@Override
public void updUser() {
System.out.println("增加用户!");
}
@Override
public void queUser() {
System.out.println("增加用户!");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans\spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<bean name="u1" class="com.xhz.springAOP.UserServiceImpl"></bean>
<bean name="myAdvice" class="com.xhz.springAOP.MyAdvice"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.xhz.springAOP.*ServiceImpl.*())" id="pc"/>
<aop:aspect ref="myAdvice">
<aop:before method="before" pointcut-ref="pc"/>
<aop:after-returning method="afterRurning" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
<aop:after-throwing method="afterException" pointcut-ref="pc"/>
<aop:after method="after" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>
package com.xhz.springAOP
import javax.annotation.Resource
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.context.ApplicationContext
import org.springframework.context.support.ClassPathXmlApplicationContext
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/xhz/springAOP/applicationContext.xml")
public class SpringAOPDemo {
@Resource(name="u1")
private UserService us
@Test
public void test(){
us.addUser()
}
}
![导入的包目录](https://img-blog.csdn.net/20180816131941154?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![junit界面](https://img-blog.csdn.net/20180816132101676?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![console界面](https://img-blog.csdn.net/20180816132134758?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
经过一番摸索:我将导入的spring-test-5.0.1换成spring-test-4.2.4程序运行就正常了:
![这里写图片描述](https://img-blog.csdn.net/20180816132734352?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zODU3NTA4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
总结:spring的spring—test的版本应该和junit的版本保持在同一个水平。