访问和更新在另一个线程

时间:2017-04-21 09:55:35

标签: java multithreading maven testng

我计划使用maven surefire插件并行执行2个自动化测试套件。

当我执行下面提供的 parallelSuiteTest.xml 时,我面临一些问题...获取空的HashMap详细信息..

在Code中我正在做的是在测试套件(Thread1)上开始我正在初始化hashmap,即 executionDetailsMap

on testMethod start(Thread2)我正在初始化另一个hashmap,即 testMethodResultsMap 并向地图添加一些数据。 现在在testMethod完成时我将thread2的 testMethodResultsMap 数据放在thread1中创建的 executionDetailsMap

同样地,我对另一个在(线程3)

中执行的testMethod做同样的事情

现在在TestSuiteFinish方法上,当我尝试访问在onSuite start(Thread1)中创建的 executionDetailsMap 时,我得到空地图

你能否指导一下我正在做的错误以及如何在主线程中获取插入的数据(线程1)

ThreadTest.java

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ThreadTest {
    @BeforeTest
    public void beforeTestMethod() {
        System.out.println("before test");
    }

    @Test
    public void testMethod() {
        synchronized (Listener.testMethodResultsMap.get()) {
            Listener.testMethodResultsMap.get().put("firstKey", "FIrstValue");
        }
    }

    @AfterTest
    public void afterTestMetho() {
        System.out.println("after test method");
    }
}

Listener.java

import java.util.HashMap;
import java.util.LinkedHashMap;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class Listener implements ITestListener, ISuiteListener {
    public static ThreadLocal<HashMap<String, LinkedHashMap<String, String>>> executionDetailsMap = null;
    public static ThreadLocal<LinkedHashMap<String, String>> testMethodResultsMap = null;


    @Override
    public void onStart(ISuite suite) {
        System.out.println("===> On Suite Start");
        executionDetailsMap = new ThreadLocal<HashMap<String, LinkedHashMap<String, String>>>() {
            @Override
            protected HashMap<String, LinkedHashMap<String, String>> initialValue() {
                return new HashMap<String, LinkedHashMap<String, String>>();
            }
        };  
        System.out.println(executionDetailsMap);
    }

    @Override
    public void onFinish(ISuite suite) {
        System.out.println("==> On Suite Finish" + executionDetailsMap);
        System.out.println(executionDetailsMap.get());
        System.out.println("------" + Thread.currentThread().getId());
        System.out.println(testMethodResultsMap);
    }

    @Override
    public void onTestStart(ITestResult result) {

        System.out.println(Thread.currentThread().getId());
        if(null!=testMethodResultsMap){ 
            synchronized (testMethodResultsMap) { } 
        } else {
            testMethodResultsMap = new ThreadLocal<LinkedHashMap<String, String>>() {
                @Override
                protected LinkedHashMap<String, String> initialValue() {
                    return new LinkedHashMap<String, String>();
                }
            };
        }

    }

    @Override
    public void onTestSuccess(ITestResult result) {
        synchronized (executionDetailsMap) {
                executionDetailsMap.get().put(Thread.currentThread().getId() +"", testMethodResultsMap.get());
        }
    }

    @Override
    public void onTestFailure(ITestResult result) {}
    @Override
    public void onTestSkipped(ITestResult result) {}
    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {}
    @Override
    public void onStart(ITestContext context) { }
    @Override
    public void onFinish(ITestContext context) { }
}

parallelSuiteTest.xml

    <?xml version="1.0" encoding="UTF-8"?>
<suite name="Parallel test suite" parallel="tests" >
    <listeners>
        <listener class-name="com.test.Listener" />
    </listeners>
    <test name="TC1" >
        <classes>
            <class name="com.test.ThreadTest" />
            <methods>
                <include name="testMethod" />
            </methods>
        </classes>
    </test>

    <test name="TC2" >
        <classes>
            <class name="com.test.ThreadTest" />
            <methods>
                <include name="testMethod" />
            </methods>
        </classes>
    </test>
</suite>

0 个答案:

没有答案