beryx / text-io

A library for creating interactive console applications in Java
http://text-io.beryx.org/
Apache License 2.0
342 stars 45 forks source link

How to use MockTextTerminal #33

Open cdprete opened 2 years ago

cdprete commented 2 years ago

Hi. Can you please provide some documentation or guidance on how to use the MockTextTerminal? I would like to unit-test my business logic and the example in the repository with the Robot is not really adequate for unit tests (given the definition of unit test).

Kind regards and thanks in advance.

siordache commented 2 years ago

Hi! What testing framework do you use? JUnit4? JUnit5? Spock? TestNG?

siordache commented 2 years ago

I put up an example project that shows how to use the MockTextTerminal with JUnit 5.

cdprete commented 2 years ago

Hi. I'm using JUnit 5, but even JUnit 4 is fine if for you is better (I can convert the code accordingly).

siordache commented 2 years ago

Did you see the example project I put up yesterday?

cdprete commented 2 years ago

Sorry, I read just the comment about the test framework and I missed the next one ;) I'll have a look at it.

Thank you.

cdprete commented 2 years ago

Hi. I can confirm the example works, but how to test prompt properties? For example, that a proper color is set?


Nevermind. I can of course fetch the properties and assert that they're there.

cdprete commented 2 years ago

Hi. I've just realized I cannot really assert what properties have been set when using executeWithPropertiesConfigurator. Do you have any way to test this?

Edit: For now, I've come up with

@Override
public void executeWithPropertiesConfigurator(Consumer<TerminalProperties<?>> propertiesConfigurator, Consumer<TextTerminal<MockTextTerminal>> action) {
    final Consumer<TerminalProperties<?>> propertiesHolder = terminalProperties -> {
        propertiesConfigurator.accept(terminalProperties);
        lastUsedProperties = new TerminalProperties<>(this);
        terminalProperties.getAllKeys().forEach(key -> lastUsedProperties.put(key, terminalProperties.getString(key)));
    };
    super.executeWithPropertiesConfigurator(propertiesHolder, action);
}

which works but it's not super nice (and not thread safe).