Spring Boot Integration Testing

1.1k views Asked by At

I have been trying to write a integration test for a small Spring boot Project that I have written but for some reason seem unable to get integration tests to work. Here is what I am trying to do:

package au.azzmosphere.integration;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {IntegrationTestConfiguration.class})
public class TestIntegration1 {
    @Test
    public void testDummy() {

    }
}

the configuration class has the following:

package au.azzmosphere.integration;

import au.azzmosphere.configuration.RobotAppConfig;
import org.springframework.context.annotation.Configuration;

@Configuration
public class IntegrationTestConfiguration extends RobotAppConfig {
}

The code is consistently getting the following errors when I try to run the Integration Test from IntelliJ

14:54:01.537 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class au.azzmosphere.integration.TestIntegration1].
14:54:01.542 [main] INFO org.springframework.test.context.TestContext - @ContextConfiguration not found for class [class au.azzmosphere.integration.TestIntegration1].
14:54:01.549 [main] INFO org.springframework.test.context.TestContextManager - @TestExecutionListeners is not present for class [class au.azzmosphere.integration.TestIntegration1]: using defaults.

From gradle the following is returned

au.azzmosphere.integration.TestIntegration1 > initializationError FAILED
    java.lang.NoClassDefFoundError
        Caused by: java.lang.ClassNotFoundException

The full stack trace is

java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionDefinition

    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:102)
    at org.springframework.test.context.TestContextManager.retrieveTestExecutionListeners(TestContextManager.java:171)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:108)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:107)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:79)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.springframework.transaction.TransactionDefinition
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more
1

There are 1 answers

0
Aaron Spiteri On

Worked it out, :) The problem was that I did not have any TransactionDefinition defined because I didn't use it. The default SpringBoot annotation attempts to inject it. The answer was to use @TestExecutionListeners({DependencyInjectionTestExecutionListener.class}) which overrides the defaults and gets things going.