2

我的建议正在正确执行并执行正确的操作,但执行两次。我希望它只执行一次。应该触发建议的方法仅执行一次,因为 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

测试NG类

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

spring-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
4

1 回答 1

-1

如果没有更多上下文,很难说这里发生了什么,但我猜你的方面不知何故被注册了两次。

于 2013-02-12T23:49:35.440 回答