1

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..

Spike Johnson
  • 365
  • 4
  • 12

1 Answers1

1

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.

Andrei Ciobanu
  • 12,500
  • 24
  • 85
  • 118
  • Thank you, with using `val()` method, I could take `String` value, from my var. But the problem is still in value of MockUnit object. I edited the answer. – Spike Johnson Nov 26 '18 at 12:27
  • @SpikeJohnson Show me how your end JSON will look like in the end (a mock) and I might be able to help you more. Only from the code, I am not sure I understand in what direction you are going. – Andrei Ciobanu Nov 26 '18 at 14:53
  • I edited the question, did you ask about that? In other project, i watching the same, even a variable, which I want make random, is a final, all time, when I appeal to it - the new random result.. – Spike Johnson Nov 26 '18 at 15:04
  • I solved the problem with good old `Random`, but you library could help me with my task more elegantly. – Spike Johnson Nov 26 '18 at 15:07
  • I'm added the same other situation. – Spike Johnson Nov 26 '18 at 15:16