Java Map的Junit测试<;字符串,Set<;对象>&燃气轮机;

Java Map的Junit测试<;字符串,Set<;对象>&燃气轮机;,java,hashmap,set,junit5,Java,Hashmap,Set,Junit5,我试图在测试用例中编写一些断言。 我的测试返回以下结果 Map<String, Set<Object>> results; class Object { String name; Set<String> elements; } String objectName = "test"; 如果能帮我解决问题,我们将不胜感激 结果包括: <"TestKey", Set<TestObject>> Te

我试图在测试用例中编写一些断言。 我的测试返回以下结果

Map<String, Set<Object>> results;

class Object {
String name;
Set<String> elements;
}

String objectName = "test";
如果能帮我解决问题,我们将不胜感激

结果包括:

<"TestKey", Set<TestObject>>
TestObject.name = objectName ;

TestObject.name=objectName;
你可以这样试试

assertThat(result.values()).hasOnlyOneElementSatisfying(r -> {
    assertThat(r).extracting(ObjectClass::getName).containsAnyOf("test1");
});
下面给出了一个示例

@Test
void sampleTest()
{
    Map<String, Set<ObjectClass>> result = new HashMap<>();
    Set<ObjectClass> objectClassSet = new HashSet<>();
    objectClassSet.add(new ObjectClass("test1", new HashSet<>()));
    objectClassSet.add(new ObjectClass("test2", new HashSet<>()));
    objectClassSet.add(new ObjectClass("test3", new HashSet<>()));
    result.put("something", objectClassSet);


    assertThat(result.values()).hasOnlyOneElementSatisfying(r -> {
        assertThat(r).extracting(ObjectClass::getName).containsAnyOf("test1");
    });
}
@测试
无效样本测试()
{
映射结果=新的HashMap();
Set objectClassSet=new HashSet();
add(新对象类(“test1”,newhashset());
add(新对象类(“test2”,newhashset());
add(新对象类(“test3”,newhashset());
result.put(“某物”,objectClassSet);
assertThat(result.values()).hasOnlyoneElementSuccessing(r->{
assertThat(r).extracting(ObjectClass::getName).containsAnyOf(“test1”);
});
}
@Test
void sampleTest()
{
    Map<String, Set<ObjectClass>> result = new HashMap<>();
    Set<ObjectClass> objectClassSet = new HashSet<>();
    objectClassSet.add(new ObjectClass("test1", new HashSet<>()));
    objectClassSet.add(new ObjectClass("test2", new HashSet<>()));
    objectClassSet.add(new ObjectClass("test3", new HashSet<>()));
    result.put("something", objectClassSet);


    assertThat(result.values()).hasOnlyOneElementSatisfying(r -> {
        assertThat(r).extracting(ObjectClass::getName).containsAnyOf("test1");
    });
}