-2

I have this hierarchy in my project:

▼ server
  ▼ myproject
    ▼ src
      ▼ main
        ▼ java
          ▼ com
            ▼ youben
              ▼ rest
                ▼ repository
                  Ⓘ MyRepository
        ▶ resources
      ▼ test
        ▼ java
          ▼ com
            ▼ youben
              ▼ rest
                Ⓒ MyRepositoryTest

This is the MyRepository interface:

public interface MyRepository extends MongoRepository<String, Integer> {
}

This is the MyRepositoryTest test class:

@DataMongoTest(properties = {"spring.mongodb.embedded.version=4.0.2"})
@ExtendWith(SpringExtension.class)
@ActiveProfiles(profiles = {"test"})
public class MyRepositoryTest {
    ...
    @Autowired MyRepository myRepository;
    ...
}

When I run the MyRepositoryTest, all of the tests are ignored and it throws an IllegalStateException. Here is the stacktrace I get:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:236)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:152)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:392)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:309)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:262)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:107)
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 org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$4(ExtensionValuesStore.java:86)
at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.computeValue(ExtensionValuesStore.java:223)
at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:211)
at org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.evaluate(ExtensionValuesStore.java:191)
at org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.access$100(ExtensionValuesStore.java:171)
at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:89)
at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:93)
at org.junit.jupiter.engine.execution.NamespaceAwareStore.getOrComputeIfAbsent(NamespaceAwareStore.java:61)
at org.springframework.test.context.junit.jupiter.SpringExtension.getTestContextManager(SpringExtension.java:294)
at org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(SpringExtension.java:113)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$10(ClassBasedTestDescriptor.java:381)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:381)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:205)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:80)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:148)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
TheStranger
  • 1,387
  • 1
  • 13
  • 35
  • Adding `@SpringBootTest` will not work. Please add the error (the **full error/stacktrace**) you initially get. You are using `@DataMongoTest` it is generally strange to also add `@ContextConfiguration` for that. THe `@ExtendWith` isn't needed. – M. Deinum Feb 15 '22 at 10:46
  • The reason why I added `@ContextConfiguration` is the following: https://stackoverflow.com/questions/71123762/how-do-i-fix-could-not-autowire-no-beans-of-myrepository-type-found I will update post with the stacktrace. – TheStranger Feb 15 '22 at 10:51
  • Ditch the `@ContextConfiguration`. The `@DataMongoTest` will detect the `@SpringBootApplication` annotated class and configure things accordingly. When doing this with your own configuration you will need to manually configure everything. Basically ditch all annotations and leave `@DataMongoTest` and `@ActiveProfiles` and re-run. – M. Deinum Feb 15 '22 at 11:05
  • Okay, I just ditched all annotations except for `@DataMongoTest` and `@ActiveProfiles`. When I run the test class it ignores all the tests and throws the following error `java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test`. – TheStranger Feb 15 '22 at 11:41
  • Btw, when I remove the `@ContextConfiguration`, I can't Autowire `MyRepository` in `MyRepositoryTest` – TheStranger Feb 15 '22 at 12:04
  • That is weird that it cannot find the configuration, it should be able to detect your `@SpringBootApplication` class and configure things accordingly. Did you have a non standard structure? if so use `@ContextConfiguration` and point it to your main class. – M. Deinum Feb 15 '22 at 14:16

1 Answers1

0

Check if you annotate your method with the following annotations solves your problem.

@ExtendWith(SpringExtension.class)
@DataMongoTest
@AutoConfigureDataJpa
@AutoConfigureTestDatabase
class MyRepositoryTest {

I thing you are missing the AutoConfigureTestDatabase to have a running database.

In addition to this, do you you have a @SpringBootApplication class in you test source? If not create one like this in test/java/your_base_package:

@SpringBootApplication

public class MyApplication {

  public static void main(String[] args) {

    SpringApplication.run(MyApplication.class, args);
  }

I successfully reproduced your problem, and after adding this it was fixed.

Also, for reference, I've the following dependencies: de.flapdoodle.embed.mongo, spring-boot-starter-data-mongodb, spring-boot-starter-test, spring-boot-test-autoconfigure, spring-test, junit-jupiter-api,junit-jupiter-engine and spring-boot-starter-jdbc.

pringi
  • 3,987
  • 5
  • 35
  • 45
  • It doesn't change anything. I've added all of the Annotations and I still get: `java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test` – TheStranger Feb 15 '22 at 12:45
  • I already have a `@SpringBootApplication` class... – TheStranger Feb 15 '22 at 14:19
  • In src our test? It needs to be in test. – pringi Feb 15 '22 at 14:20
  • It was in the src of main, not test. I created one in test and it added the `@ContextConfiguration()` and had it pointing at the new `@SpringBootApplication` from the test source, and that fixed it. What I don't understand is that in my previous project the test in the test src had no problem pointing at the `@SpringBootApplication` in the main src. Why is that? – TheStranger Feb 15 '22 at 14:31