我的建议正确执行并执行正确的操作,但执行两次除外。我希望它只执行一次。由于startTestSuite标题仅在日志中打印一次,因此应该触发建议的方法仅执行一次。 Bean和上下文是在TestNG类中生成的。我尝试在initSpring()方法上使用@BeforeClass和@BeforeSuite标记运行它,结果相同。
进一步背景:
这样做的目的是获得测试套件启动和结束时间的时间戳,并获得各个测试开始和结束时间的时间戳。最终,我将捕获测试失败时的堆栈跟踪,以便我们可以构建测试自动化问题最多的模式,并允许我们将精力集中在需要修复的更重要的自动化领域而不是修理琐碎的事情。
日志文件
[INFO] 2013-02-11 17:56:07.646-0800 test.ui.tests.BVT.initSpring: context object instantiated
[INFO] 2013-02-11 17:56:07.647-0800 test.ui.tests.BVT.initSpring: Shutdown hook registered
[INFO] 2013-02-11 17:56:07.647-0800 test.ui.tests.BVT.initSpring: Obtained a Instrumentation proxy
[INFO] 2013-02-11 17:56:07.661-0800 instrumentation.dao.implement.TestSuiteDaoImpl.beforeTestSuite: ***************Running Advice: beforeTestSuite: startTestSuite
[INFO] 2013-02-11 17:56:07.948-0800 instrumentation.dao.implement.TestSuiteDaoImpl.beforeTestSuite: ***************Running Advice: beforeTestSuite: startTestSuite
[INFO] 2013-02-11 17:56:07.952-0800 test.ui.tests.InstrumentationImpl.startTestSuite: Going to print the title: startTestSuite
[INFO] 2013-02-11 17:56:07.952-0800 test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-11 17:56:07.952-0800 test.ui.helpers.TitleLogger.testTitle: ** startTestSuite **
[INFO] 2013-02-11 17:56:07.953-0800 test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-11 17:56:07.953-0800 test.ui.tests.BVT.initSpring: Called the Proxy
TestNG Class
public class BVT extends SeleniumTest {
@BeforeSuite
public void initSpring() {
titleLog.testTitle("initSpring");
context = new FileSystemXmlApplicationContext(new String[]{"spring-beans.xml"});
Assert.assertNotNull(context, "Unable to load spring-beans.xml");
logger.info("context object instantiated");
context.registerShutdownHook();
logger.info("Shutdown hook registered");
instrument = (Instrumentation) context.getBean("InstrumentationProxy");
Assert.assertNotNull(instrument, "Unable to create a Instrumentation Proxy");
logger.info("Obtained a Instrumentation proxy");
instrument.startTestSuite();
logger.info("Called the Proxy");
}
}
代理接口
public interface Instrumentation {
public void startTestSuite();
}
被叫代理方法
@Override
public void startTestSuite() {
logger.info("Going to print the title: startTestSuite");
titleLog.testTitle("startTestSuite");
}
建议
@Override
@Before("execution(void test.ui.tests.InstrumentationImpl.startTestSuite())")
public void beforeTestSuite(JoinPoint jp) {
logger.info("***************Running Advice: " + jp.getSignature().getName());
DateTimeHelper dth = new DateTimeHelper();
TestSuite ts = new TestSuite();
//Get the current time
Timestamp start = dth.getCurrentSqlTimestamp();
//Update the object with the starting time
ts.setTestSuiteStart(start);
//Commit to the database
saveTestSuite(ts);
}
弹簧-beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="instrumentation.dao.implement" />
<context:annotation-config />
<aop:aspectj-autoproxy />
<bean id="basicDS" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driver}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="url" value="${url}" />
</bean>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:datasource.properties" />
</bean>
<bean id="testSuite" class="instrumentation.dao.implement.TestSuiteDaoImpl" autowire="constructor" />
<bean id="Instrumentation" class="test.ui.tests.InstrumentationImpl" />
<bean id="InstrumentationProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<aop:scoped-proxy proxy-target-class="false"/>
<property name="target" ref="Instrumentation" />
</bean>
更新: 我将我的切入点改为此(Insturmentation vs InstrumentationImpl)并且它被触发了四次。
@Before("execution(void test.ui.tests.Instrumentation.startTestSuite())")
以下是添加了jp.getTarget()。toString()的日志。
[INFO] 2013-02-13 14:51:30.258-0800 amazon.omaha.test.ui.tests.BVT.initSpring: context object instantiated
[INFO] 2013-02-13 14:51:30.259-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Shutdown hook registered
[INFO] 2013-02-13 14:51:30.270-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Obtained a Instrumentation proxy
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** afterTestSuite Advice Method startTestSuite **
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.276-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.277-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** amazon.omaha.test.ui.tests.InstrumentationImpl@cb754f **
[INFO] 2013-02-13 14:51:30.278-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.573-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.575-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** afterTestSuite Advice Method startTestSuite **
[INFO] 2013-02-13 14:51:30.575-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: *************************************************
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** amazon.omaha.test.ui.tests.InstrumentationImpl@cb754f **
[INFO] 2013-02-13 14:51:30.576-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ***********************************************************
[INFO] 2013-02-13 14:51:30.581-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ** startTestSuite **
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.helpers.TitleLogger.testTitle: ********************
[INFO] 2013-02-13 14:51:30.582-0800 amazon.omaha.test.ui.tests.BVT.initSpring: Called the Proxy
答案 0 :(得分:-1)
如果没有更多的背景,很难说这里发生了什么,但我的猜测是你的方面已经被注册了两次。