Hi ,

We are getting the below error while executing one of the Junit test class

java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getAnnotation(SpringBootTestContextBootstrapper.java:322)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getClasses(SpringBootTestContextBootstrapper.java:312)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.resolveContextLoader(SpringBootTestContextBootstrapper.java:128)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:348)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:294)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:108)
    at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:102)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:137)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:122)
    at cucumber.runtime.java.spring.CucumberTestContextManager.<init>(SpringFactory.java:206)
    at cucumber.runtime.java.spring.SpringFactory.start(SpringFactory.java:102)
    at cucumber.runtime.java.JavaBackend.buildWorld(JavaBackend.java:123)
    at cucumber.runtime.Runtime.buildBackendWorlds(Runtime.java:141)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:38)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:102)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.runtime.junit.ExamplesRunner.run(ExamplesRunner.java:59)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.runtime.junit.ScenarioOutlineRunner.run(ScenarioOutlineRunner.java:53)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:95)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:38)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:100)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.TestContextAnnotationUtils
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 60 more

Because of the company policy I'm just pasting here sample code. Let me know if any details needed, Will provide

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AutopayPostHandlerService.class, properties = {
        "spring.cloud.config.enabled=false","ap.sendletter.enable=true"
})
public class AutopayPostHandlerServiceTest {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Autowired
    private AutopayPostHandlerService autopayPostHandlerService;



    @Before
    public void setup(){

        ccAccountBuilder = new CCAccountBuilder();
        creditCardAccount = ccAccountBuilder.withCountryCode("CA").withCoBrandID("1").withAccountCycleDay(20).build();

        mongoRequest = getMongoRequest();

        ReflectionTestUtils.setField(autopayPostHandlerService,"eligibleCountryCodes", "CA");
        ReflectionTestUtils.setField(autopayPostHandlerService,"eligibleCoBrandIds", "1");
    }

    @Test
    public void testGetFormattedEnrolmentDate() {
        Date date = new Date();
        String formattedEnrolmentDate = autopayPostHandlerService.getFormattedEnrolmentDate(date);
        String regex = "^\\d{4}-\\d{2}-\\d{2}$";
        assertTrue("formattedEnrolmentDate does not match regex",formattedEnrolmentDate.matches(regex));
    }

Comment From: wilkinsona

TestContextAnnotationUtils is new in Spring Framework 5.3 so I suspect that you have an old version of Spring Framework's spring-test module on the classpath. If you have any further questions, please follow up on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.