Unable to Find @SpringBootConfiguration with @DataJpaTest – 无法通过@DataJpaTest找到@SpringBootConfiguration

最后修改: 2019年 10月 14日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In our tutorial on testing in Spring Boot, we saw how we can use the @DataJpaTest annotation.

在我们关于在Spring Boot中进行测试的教程中,我们看到了如何使用@DataJpaTest注释。

In this next tutorial, we’ll see how to resolve the error “Unable to find a @SpringBootConfiguration.

在接下来的教程中,我们将看到如何解决 “无法找到@SpringBootConfiguration“的错误,

2. Causes

2.原因

The @DataJpaTest annotation helps us to set up a JPA test. For this, it initializes the application, ignoring irrelevant parts. For instance, it’ll ignore MVC controllers.

@DataJpaTest注解帮助我们建立一个JPA测试。为此,它初始化应用程序,忽略不相关的部分。例如,它将忽略MVC控制器。

However, to initialize the application it needs configuration.

然而,为了初始化应用程序,它需要配置。

For this, it searches in the current package and goes up in the package hierarchy until a configuration is found.

为此,它在当前包中搜索,并在包的层次结构中向上搜索,直到找到一个配置。

For example, let’s add a @DataJpaTest in the com.baeldung.data.jpa package. Then, it’ll search for a configuration class in:

例如,让我们在com.baeldung.data.jpa package.中添加一个@DataJpaTest,然后,它将搜索一个配置类在。

  • com.baeldung.data.jpa
  • com.baeldung.data
  • and so on

However, when no configuration is found, the application will report an error:

然而,当没有找到配置时,应用程序将报告一个错误。

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

This could, for example, happen because the configuration class is located in a more specific package, like com.baeldung.data.jpa.application.

例如,这可能是因为配置类位于一个更具体的包中,比如com.baeldung.data.jpa.application

Let’s move the configuration class to com.baeldung.data.jpa. As a result, Spring will now be able to find it.

让我们把配置类移到com.baeldung.data.jpa。这样一来,Spring就能找到它了。

On the other hand, we can have a module that doesn’t have any @SpringBootConfiguration. In the next section, we’ll look into this scenario.

另一方面,我们可以有一个没有任何@SpringBootConfiguration的模块。在下一节,我们将研究这种情况。

3. Missing @SpringBootConfiguration

3.缺少@SpringBootConfiguration

What if our module doesn’t contain any @SpringBootConfiguration? There can be multiple reasons for that. Let’s assume, for this tutorial, that we have a module containing only model classes.

如果我们的模块不包含任何@SpringBootConfiguration怎么办?这可能有多种原因。让我们假设,在本教程中,我们有一个只包含模型类的模块。

So, the solution is straightforward. Let’s add a @SpringBootApplication to our test code:

因此,解决方案是直接的。让我们在测试代码中添加一个@SpringBootApplication

@SpringBootApplication
public class TestApplication {}

Now that we have an annotated class, Spring is able to bootstrap our tests.

现在我们有了一个注解的类,Spring就能够引导我们的测试了。

To validate our setup, let’s inject a TestEntityManager and validate that it is set:

为了验证我们的设置,让我们注入一个TestEntityManager并验证它是否被设置。

@RunWith(SpringRunner.class)
@DataJpaTest
public class DataJpaUnitTest {

    @Autowired
    TestEntityManager entityManager;

    @Test
    public void givenACorrectSetup_thenAnEntityManagerWillBeAvailable() {
        assertNotNull(entityManager);
    }
}

This test succeeds when Spring can find the @SpringBootConfiguration in its own package or one of its parent packages.

Spring可以在自己的包或其父包中找到@SpringBootConfiguration时,该测试成功。

4. Conclusion

4.结论

In this short tutorial, we looked into two different causes for the error: “Unable to find a @SpringBootConfiguration“.

在这个简短的教程中,我们研究了导致错误的两个不同原因。”无法找到@SpringBootConfiguration“。

First, we looked at a case where the configuration class could not found. This was because of its location. We solved it by moving the configuration class to another location.

首先,我们看了一个无法找到配置类的案例。这是因为它的位置问题。我们通过将配置类移到另一个位置来解决这个问题。

Second, we looked at a scenario where no configuration class was available. We resolved this by adding a @SpringBootApplication to our test codebase.

第二,我们看了一个没有配置类的场景。我们通过在测试代码库中添加一个@SpringBootApplication来解决这个问题。

As always, the full source code of the article is available over on GitHub.

一如既往,该文章的完整源代码可在GitHub上获得