Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

java - Junit test for Map<String, Set<Object>>

I'm trying to write few asserts in a test case. My test returns the following.

Map<String, Set<Object>> results;

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

String objectName = "test";

I tried the following assert but getting an error cannot resolve method name getName() in Set.

assertThat(result.values()).hasOnlyOneElementSatisfying(r -> {
        assertThat(r.getName().isEqualTo(objectName);
});

Any help with where i'm going wrong will be much appreciated.

Results contains:

<"TestKey", Set<TestObject>>
TestObject.name = objectName ;
question from:https://stackoverflow.com/questions/65874289/junit-test-for-mapstring-setobject

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try like this

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

One sample example is given below

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...