Suppose there is an object that can be in n different states and move from one state to another. Not all state changes are valid. For unit testing of these transitions, we need n * n methods (transition from stateN to stateN ). In fact, we have only two test cases - allowed and notallowed.
@org.junit.Test(expected = IllegalStateException.class) public void switchFromState$N$ToState$K$() { testObj = new Test($N$); // Π½Π΅ Π΄ΠΎΠΏΡΡΡΠΈΠΌΠΎ Π»ΠΎΠ³ΠΈΠΊΠΎΠΉ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ testObj.switchToState$K$(); } public void switchFromState$N$ToState$K$() { testObj = new Test($N$); // Π²ΡΠ·ΠΎΠ² ΡΡΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π° Π½Π΅ Π±ΡΠΎΡΠΈΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ testObj.switchToState$K$(); assertEquals("iam in state$K$", testObj.toString()); } With n = 4 (16 tests) it is already dreary to type even with live templates. How can you quickly solve this problem without leaving the studio?
Update
At the moment, I solved the problem by generating code through javascript.
const transactionRules = [ allowed("State1", "State1"), forbidden("State1", "State2"), allowed("State1", "State3"), allowed("State1", "State1"), // ... forbidden("State5", "State4"), ]; function allowed(currentState, targetState) { return () => `@Test public void switchFrom${currentState}To${targetState}Test() { testObj = new Test(${currentState}); // Π²ΡΠ·ΠΎΠ² ΡΡΠΎΠ³ΠΎ ΠΌΠ΅ΡΠΎΠ΄Π° Π½Π΅ Π±ΡΠΎΡΠΈΡ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅ testObj.switchTo${targetState}(); assertEquals("iam in ${targetState}", testObj.toString()); } `; } function forbidden(currentState, targetState) { return () => `@Test public void switchFrom${currentState}To${targetState}Test() { testObj = new Test(${currentState}); // Π½Π΅ Π΄ΠΎΠΏΡΡΡΠΈΠΌΠΎ Π»ΠΎΠ³ΠΈΠΊΠΎΠΉ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ testObj.switchTo${targetState}(); } `; } transactionRules.forEach((rule) => console.log(rule())); If we run this script, we get the code for our methods. But writing java-methods through template strings is not very convenient. I would like to achieve the same results without leaving the IDE.