2

我想将以下结构转储到 YAML 文件中:

public class TestSuite {
    String name
    List testCases = []
}

测试用例列表是此类:

class TestCase {
    String name
    String id
}

我希望它看起来像这样:

name: Carrier Handling and Traffic
testCases:
- name: Call setup by UE
  id: DCM00000001

但它最终看起来像这样:

name: Carrier Handling and Traffic
testCases:
- !!com.package.path.TestCase
  name: Call setup by UE
  id: DCM00000001

我想这与 List 不是标记数据结构这一事实有关,但我不知道如何获得测试用例的名称来表示对象。尖端?

4

1 回答 1

5

是否定义TestSuite为:

public class TestSuite {
    String name
    List<TestCase> testCases = []
}

让你更接近你想要的结果?虽然我自己没有使用过 SnakeYaml ......


编辑

有一些空闲时间,并想出了这个独立的测试脚本:

@Grab( 'org.yaml:snakeyaml:1.10' )
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.representer.Representer
import java.beans.IntrospectionException
import org.yaml.snakeyaml.introspector.Property

public class TestSuite {
    String name
    List<TestCase> testCases = []
}

class TestCase {
    String name
    String id
}

class NonMetaClassRepresenter extends Representer {
  protected Set<Property> getProperties( Class<? extends Object> type ) throws IntrospectionException {
    super.getProperties( type ).findAll { it.name != 'metaClass' }
  }
}

TestSuite suite = new TestSuite( name:'Carrier Handling and Traffic' )
suite.testCases << new TestCase( name:'Call setup by UE', id:'DCM00000001' ) 

println new Yaml( new NonMetaClassRepresenter() ).dumpAsMap( suite )

哪个打印:

name: Carrier Handling and Traffic
testCases:
- id: DCM00000001
  name: Call setup by UE
于 2012-05-01T10:06:10.743 回答