Spike Johnson
Spike Johnson

Reputation: 365

Can't cast MockUnit to String with mockneat

Try to use my own Enum like mock with help of library mockneat. But the problem is that I can't take string value from MockUnit object for my right working of switch loop.

It's my Enum:

public enum SubType {

    ROUTER("router"),
    SERVER("server"),

    private final String value;

    SubType(final String newValue) {
    value = newValue;
    }

    public String getValue() {
    return value;
    }
}

And it's my Build class, where I try to use custom values from SubType:

public class Build {

private MockNeat mockNeat = MockNeat.threadLocal();
private ArrayList<String> group = new ArrayList<>();

public void addToList() {

    group.add("All Devices");
    group.add("All Groups");

    for (i = 0; i < 10; i++) {

        MockUnit subType =  mockNeat.from(SubType.class).map(SubType::getValue);

//EDITED:
//For example there are sout(subType) = "router"

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = mockNeat.reflect(Build.class)
                .field("group", constructGroupField(subType, group))
                .map(gson::toJson)
                .val();

        System.out.println(json);
    }
}

private ArrayList<String> constructGroupField(MockUnit subType, ArrayList<String> group) {

    //System.out.println(subType);
    //Output: net.andreinc.mockneat.abstraction.MockUnit$$Lambda$22/902830499@740773a3
    //But how I can get there my string like "router" or ""server"
    //How I can cast MockUnit to String value?

//EDITED:
// And there it can be already sout(subType)= "server"
    switch (subType.toString()) {

        case "router":
            group.add("All AAA");
            break;
        case "server":
            group.add("All BBB");
            break;
    }

    return group;
}

}

The main goal is to, depending on which string be in the subtype field, output the corresponding json file

UPDATED:

The final Json file, that I want to produce:

{
    "product": "a",
    "group": "router"
}

Another situation:

private String someMethod() {

        MockUnitInt localId = mockNeat.ints().range(5000, 35_000);

        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        return mockNeat.reflect(someEntity.class)
                       .field("deviceLocalID", localId.mapToString())
                       .field("localID", localId.mapToString())
                       .map(gson::toJson)
                       .val();

    }

Even the localId was a final field in class, all time when something refer to it, it's give random value each time. Can't understand what wrong with that..

Upvotes: 1

Views: 260

Answers (1)

Andrei Ciobanu
Andrei Ciobanu

Reputation: 12848

I think the answer is to change the switch line to:

switch (subType.val().toString())

The idea is that any MockUnit is not returning any value until you explicitly call val() on it.

But it's still not clear for me what you want to achieve? How do you want your final json to look like ?

PS: I might help, I am the author of MockNeat.

Upvotes: 1

Related Questions