-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb30ab3
commit a973352
Showing
6 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/test/java/inheritance/fromanabstracttest/AllTheBehaviours.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package inheritance.fromanabstracttest; | ||
|
||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public abstract class AllTheBehaviours { | ||
|
||
/* | ||
Most examples for inheritence in testing are the use of a Base test class | ||
for common BeforeAll and AfterAll functions | ||
Another very useful approach is to have a set of @Test methods | ||
that you want to apply to multiple type of objects e.g. | ||
- run the same tests locally and on dev | ||
- run the same tests on different database types | ||
- run the same tests on a system with slightly different configuration | ||
In this example I have two objects that implement the same interface | ||
and they are supposed to do the same thing. But they take different | ||
approaches to the implementation. | ||
By making the base test abstract, it will not be instantiated | ||
normally or run when the tests are run | ||
Each test class will override the `getObjectUnderTest` method | ||
and inherit all the @Test methods so they only have to be run once. | ||
In IntelliJ if I try to run this test it will instead prompt me to run | ||
either or all of the Test classes that extend this. | ||
Similarly when I run this using `mvn test` the `AllTheBehaviours` will not run | ||
but the `Test` classes which extend it will. | ||
*/ | ||
public MyTextData getObjectUnderTest(){ | ||
return null; | ||
} | ||
|
||
@Test | ||
public void canRemoveSpaces(){ | ||
|
||
MyTextData td = getObjectUnderTest(); | ||
|
||
Assert.assertEquals("therearenospaceshere", | ||
td.removeSpacesFrom("there are no spaces here")); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/java/inheritance/fromanabstracttest/ApproachOneTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package inheritance.fromanabstracttest; | ||
|
||
public class ApproachOneTest extends AllTheBehaviours{ | ||
|
||
public MyTextData getObjectUnderTest(){ | ||
return new TextDataApproachOne(); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/test/java/inheritance/fromanabstracttest/ApproachTwoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package inheritance.fromanabstracttest; | ||
|
||
public class ApproachTwoTest extends ApproachOneTest { | ||
|
||
public MyTextData getObjectUnderTest(){ | ||
return new TextDataApproachTwo(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package inheritance.fromanabstracttest; | ||
|
||
public interface MyTextData { | ||
String removeSpacesFrom(String text); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/test/java/inheritance/fromanabstracttest/TextDataApproachOne.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package inheritance.fromanabstracttest; | ||
|
||
public class TextDataApproachOne implements MyTextData { | ||
|
||
// first set of approaches to generating data | ||
|
||
@Override | ||
public String removeSpacesFrom(String text) { | ||
return text.replace(" ", ""); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/java/inheritance/fromanabstracttest/TextDataApproachTwo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package inheritance.fromanabstracttest; | ||
|
||
public class TextDataApproachTwo implements MyTextData { | ||
@Override | ||
public String removeSpacesFrom(String text) { | ||
StringBuilder retString = new StringBuilder(); | ||
|
||
for(char t : text.toCharArray() ){ | ||
if(t != ' '){ | ||
retString.append(t); | ||
} | ||
} | ||
return retString.toString(); | ||
} | ||
} |