问题描述
package com.parallel;
import org.testng.annotations.Test;
public class TestParallelOne {
@Test
public void testCaSEOne() {
//Printing Id of the thread on using which test method got executed
System.out.println("Test Case One with Thread Id:- "
+ Thread.currentThread().getId());
test();
}
@Test
public void testCaseTwo() {
////Printing Id of the thread on using which test method got executed
System.out.println("Test Case two with Thread Id:- "
+ Thread.currentThread().getId());
test();
}
public synchronized void test()
{
for(int i =0; < 10; i++) System.out.println(i);
}
}
和以下suite.xml
<!DOCTYPE suite SYstem "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test suite" parallel="methods" thread-count="2">
<test name="Regression 1">
<classes>
<class name="com.parallel.TestParallelOne"/>
</classes>
</test>
</suite>
我的期望是测试将并行运行,但是第一个获得锁的测试将保持它直到完成该方法(对synchronized块的期望相同),但是完全忽略了syncy关键字... 这是什么问题同步方法解决方案是针对我的特定情况的最佳解决方案,但似乎TestNG缺少了什么?
解决方法
尝试锁定静态对象
package com.parallel;
导入org.testng.annotations.Test;
公共类TestParallelOne {
private static String LOCK = "lock";
@Test
public void testCaseOne() {
System.out.println("Test Case One with Thread Id:- " +
Thread.currentThread().getId());
test();
}
@Test
public void testCaseTwo() {
System.out.println("Test Case two with Thread Id:- "
+ Thread.currentThread().getId());
test();
}
public void test()
{
synchronized(LOCK){
for(int i =0; < 10; i++) System.out.println(i);
}
}
}