Skip to content

Instantly share code, notes, and snippets.

@DarrenForsythe
Created June 11, 2020 02:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarrenForsythe/5ffa262cd527c07a9e7318b3a146517f to your computer and use it in GitHub Desktop.
Save DarrenForsythe/5ffa262cd527c07a9e7318b3a146517f to your computer and use it in GitHub Desktop.
Junit 5 migration script
#!/bin/sh
FILE=pom.xml
if [[ -f "$FILE" ]]; then
echo "$FILE found. Running Junit conversion. Note this is not a complete movement script. You will need\
to do some manual tweaks to your code base"
#Move to the new Test Annotation
echo "Moving to Jupiter Test Annotation"
git grep -l 'org.junit.Test' | xargs sed -i '' -e 's/org.junit.Test/org.junit.jupiter.api.Test/g'
#Move to new Before Annotation
echo "Moving Before to BeforeEach"
git grep -l '@Before' | xargs sed -i '' -e 's/@Before/@BeforeEach/g'
echo "Moving Before Import to Jupiter BeforeEach"
git grep -l 'org.junit.Before' | xargs sed -i '' -e 's/org.junit.Before/org.junit.jupiter.api.BeforeEach/g'
#SpringRunner is a JUnit4 API
#The replacement is @ExtendWith(SpringExtension.class) but this is included in slice tests
#e.g. WebMvcTest, DataJpaTest, JacksonTest etc. and SpringBootTest. Only required if using something like
#@ContextConfiguration
echo "Removing RunWith(SpringRunner.class) - The replacement is @ExtendWith(SpringExtension.class) but this is included in slice tests\\
e.g. WebMvcTest, DataJpaTest, JacksonTest etc. and SpringBootTest. Only required if using something like @ContextConfiguration"
git grep -l '@RunWith(SpringRunner.class)' | xargs sed -i '' -e 's/@RunWith(SpringRunner.class)//g'
#Replace BeforeClass with Before all, note this gets caught in moving @BeforeEach so its... BeforeEachClass we look for
echo "Moving BeforeClass to Jupiter BeforeAll"
git grep -l '@BeforeEachClass' | xargs sed -i '' -e 's/@BeforeEachClass/@BeforeAll/g'
echo "Moving BeforeAll import to Jupiter BeforeAll"
git grep -l 'org.junit.BeforeAll' | xargs sed -i '' -e 's/org.junit.BeforeAll/org.junit.jupiter.api.BeforeAll/g'
echo "Moving Ignore to Disabled"
git grep -l '@Ignore' | xargs sed -i '' -e 's/@Ignore/@Disabled/g'
echo "Moving Ignore Import to Jupiter Disabled"
git grep -l 'org.junit.Ignore' | xargs sed -i '' -e 's/org.junit.Ignore/org.junit.jupiter.api.Disabled/g'
#Move to test sources
TESTDIR=src/test
if [[ -d "$TESTDIR" ]]; then
echo "$TESTDIR src/test directory found - updating to package private"
cd src/test
#JUnit 5 can access package private classes and fields. Public is no longer required
#Note - This can break helper/util classes.
echo "Removing public modifier in favour of package private. JUnit5 no longer requires public access for classes or methods"
git grep -l 'public ' | xargs sed -i '' -e 's/public //g'
exit 0
fi
echo "no test directory found"
exit 0
else
echo "$FILE not found, not in root of project. Exiting."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment