-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#patch: Enable snapshot-testing in a superclass
Tests can now use `Expect` defined in a superclass
- Loading branch information
1 parent
ddfd6ee
commit e9521ab
Showing
12 changed files
with
162 additions
and
22 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/utils/ReflectionUtils.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,48 @@ | ||
package au.com.origin.snapshots.utils; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Optional; | ||
import java.util.function.Predicate; | ||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class ReflectionUtils { | ||
|
||
/** | ||
* Find {@link Field} by given predicate. | ||
* | ||
* <p>Invoke the given predicate on all fields in the target class, going up the class hierarchy | ||
* to get all declared fields. | ||
* | ||
* @param clazz the target class to analyze | ||
* @param predicate the predicate | ||
* @return the field or empty optional | ||
*/ | ||
public static Optional<Field> findFieldByPredicate( | ||
final Class<?> clazz, final Predicate<Field> predicate) { | ||
Class<?> targetClass = clazz; | ||
|
||
do { | ||
final Field[] fields = targetClass.getDeclaredFields(); | ||
for (final Field field : fields) { | ||
if (!predicate.test(field)) { | ||
continue; | ||
} | ||
return Optional.of(field); | ||
} | ||
targetClass = targetClass.getSuperclass(); | ||
} while (targetClass != null && targetClass != Object.class); | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
public static void makeAccessible(final Field field) { | ||
if ((!Modifier.isPublic(field.getModifiers()) | ||
|| !Modifier.isPublic(field.getDeclaringClass().getModifiers()) | ||
|| Modifier.isFinal(field.getModifiers())) | ||
&& !field.isAccessible()) { | ||
field.setAccessible(true); | ||
} | ||
} | ||
} |
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
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
23 changes: 23 additions & 0 deletions
23
java-snapshot-testing-junit4/src/test/java/au/com/origin/snapshots/BaseClassTest.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,23 @@ | ||
package au.com.origin.snapshots; | ||
|
||
import au.com.origin.snapshots.junit4.SnapshotRunner; | ||
import org.junit.Test; | ||
import org.junit.experimental.runners.Enclosed; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(Enclosed.class) | ||
public class BaseClassTest { | ||
|
||
static class TestBase { | ||
Expect expect; | ||
} | ||
|
||
@RunWith(SnapshotRunner.class) | ||
public static class NestedClass extends TestBase { | ||
|
||
@Test | ||
public void helloWorldTest() { | ||
expect.toMatchSnapshot("Hello World"); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...junit4/src/test/java/au/com/origin/snapshots/__snapshots__/BaseClassTest$NestedClass.snap
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,3 @@ | ||
au.com.origin.snapshots.BaseClassTest$NestedClass.helloWorldTest=[ | ||
Hello World | ||
] |
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
24 changes: 24 additions & 0 deletions
24
java-snapshot-testing-junit5/src/test/java/au/com/origin/snapshots/BaseClassTest.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,24 @@ | ||
package au.com.origin.snapshots; | ||
|
||
import au.com.origin.snapshots.junit5.SnapshotExtension; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@ExtendWith({SnapshotExtension.class}) | ||
public class BaseClassTest { | ||
|
||
class TestBase { | ||
Expect expect; | ||
} | ||
|
||
@Nested | ||
@ExtendWith(SnapshotExtension.class) | ||
class NestedClass extends TestBase { | ||
|
||
@Test | ||
public void helloWorldTest() { | ||
expect.toMatchSnapshot("Hello World"); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...junit5/src/test/java/au/com/origin/snapshots/__snapshots__/BaseClassTest$NestedClass.snap
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,3 @@ | ||
au.com.origin.snapshots.BaseClassTest$NestedClass.helloWorldTest=[ | ||
Hello World | ||
] |
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
10 changes: 10 additions & 0 deletions
10
java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/SpecificationBase.groovy
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,10 @@ | ||
package au.com.origin.snapshots | ||
|
||
import org.junit.runner.RunWith | ||
import org.spockframework.runtime.Sputnik | ||
import spock.lang.Specification | ||
|
||
@RunWith(Sputnik.class) | ||
class SpecificationBase extends Specification { | ||
Expect expect; | ||
} |
18 changes: 18 additions & 0 deletions
18
java-snapshot-testing-spock/src/test/groovy/au/com/origin/snapshots/TestBaseSpec.groovy
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,18 @@ | ||
package au.com.origin.snapshots | ||
|
||
import au.com.origin.snapshots.annotations.SnapshotName | ||
import au.com.origin.snapshots.spock.EnableSnapshots | ||
|
||
@EnableSnapshots | ||
class TestBaseSpec extends SpecificationBase { | ||
|
||
@SnapshotName("Should use extension") | ||
def "Should use extension"() { | ||
when: | ||
expect.toMatchSnapshot("Hello World") | ||
|
||
then: | ||
true | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...hot-testing-spock/src/test/groovy/au/com/origin/snapshots/__snapshots__/TestBaseSpec.snap
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,3 @@ | ||
Should use extension=[ | ||
Hello World | ||
] |