Skip to content

Commit

Permalink
added abstract example
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltester committed Jan 22, 2024
1 parent bb30ab3 commit a973352
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/test/java/inheritance/fromanabstracttest/AllTheBehaviours.java
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"));
}
}
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();
}

}
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();
}

}
5 changes: 5 additions & 0 deletions src/test/java/inheritance/fromanabstracttest/MyTextData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package inheritance.fromanabstracttest;

public interface MyTextData {
String removeSpacesFrom(String text);
}
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(" ", "");
}
}
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();
}
}

0 comments on commit a973352

Please sign in to comment.