I'm building a java fluent API like this one
new Job().doFirst().doAfterFirst().thisOne();
new Job().doFirst().doAfterFirst().orThisOne();
I can do it by defining some classes / interfaces like
public class Job {
DoAfterFirst doFirst();
}
public interface DoAfterFirst {
FinishWith doAfterFirst();
}
public interface FinishWith {
void thisOne();
void orThisOne();
}
Because it is a DSL and not a Builder, I would like to tests the order of the methods. For example I would like to test that if I called doAfterFirst() I can now only call thisOne() and orThisOne(). I was thinking about something like (but I can successfully achieve that)
@Test
public void testDoAfterFirstChoice() {
String expectedResults = new String[] {"thisOne","orThisOne"};
assertTrue(new Job().doFirst().doAfterFirst().availableChoices().equals(expectedResults));
}
Currently I have junit tests to tests each of this method, but this tests are not able to check the order. I manually check that the order is right but I would like to write it as tests so I will be able to rerun them if I extend my DSL.
How should I test this point (the order) ? Is unit tests the right way to do it ? Are there some libraries to do it ?
EDIT
The aim of the tests is to validate the DSL. If the DSL is correct, I'm sure that after doAfterFirst() I will have thisOne() and orThisOne() (because the compiler will check that).
This example is simple, but most of the time DSL is larger with mandatory, optional and repeatable methods. In this case when you add a functionality to your DSL, you can broke some other rules. I want my tests to check that nothing is broken.
don't do it. the beauty of static languages is that compiler does a lot of testing for you. after doAfterFirst() you can call only thisOne() or orThisOne() because it's defined by the interface. and compiler will check it at each compilation. there is simply no other possibility your code compiles. what other order would you expect? are you afraid that someone will call thisOne() and later doFirst()? compiler won't allow that
instead test the result of your whole fluent DSL execution. check if code inside your builder is correct. but don't test the possible methods at each step
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With