0

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
  • can you paste fulll stack trace? Which class is not found? – Jaydeep Rajput Sep 04 '17 at 05:07
  • I have added the full stack trace to the answer hope it helps. – Aaron Spiteri Sep 04 '17 at 05:16
  • CHeck this https://www.mkyong.com/spring/java-lang-classnotfoundexception-org-springframework-transaction-transactionexception/ – gladiator Sep 04 '17 at 05:22
  • Looks like the TestExecutionListeners automatically puts the TransactionalTestExecutionListener into the application if you the 'TestExecutionListeners' annotation is not used. I have now added the following – Aaron Spiteri Sep 04 '17 at 05:31
  • `@TestExecutionListeners({DependencyInjectionTestExecutionListener.class})` and it has gotten rid of the original exception but has introduced a new one – Aaron Spiteri Sep 04 '17 at 05:32
  • ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@68837a77] to prepare test instance [au.azzmosphere.integration.TestIntegration1@2d554825] java.lang.IllegalArgumentException: Can not build an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration. – Aaron Spiteri Sep 04 '17 at 05:32
  • I thought that this was taken care of by the SpringBoot annotation but may be wrong. – Aaron Spiteri Sep 04 '17 at 05:32
  • @gladiator I checked out that MyKong page, attempted to apply the suggestion and it worked some what, but still just progresses to the next exception that I am getting :( which is `16:03:56.893 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[TestContext@3dd3bcd testClass = TestIntegration1, locations = array['classpath:/au/azzmosphere/integration/TestIntegration1-context.xml'], testInstance =` – Aaron Spiteri Sep 04 '17 at 06:05
  • Ok finally got somewhere, I removed the dependency injections all together. – Aaron Spiteri Sep 04 '17 at 06:18
  • `@TestExecutionListeners({})` and it now executes the dummy test, – Aaron Spiteri Sep 04 '17 at 06:20

1 Answers1

0

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.