<i id='Dr6ap'><tr id='Dr6ap'><dt id='Dr6ap'><q id='Dr6ap'><span id='Dr6ap'><b id='Dr6ap'><form id='Dr6ap'><ins id='Dr6ap'></ins><ul id='Dr6ap'></ul><sub id='Dr6ap'></sub></form><legend id='Dr6ap'></legend><bdo id='Dr6ap'><pre id='Dr6ap'><center id='Dr6ap'></center></pre></bdo></b><th id='Dr6ap'></th></span></q></dt></tr></i><div id='Dr6ap'><tfoot id='Dr6ap'></tfoot><dl id='Dr6ap'><fieldset id='Dr6ap'></fieldset></dl></div>

      • <bdo id='Dr6ap'></bdo><ul id='Dr6ap'></ul>
      <tfoot id='Dr6ap'></tfoot><legend id='Dr6ap'><style id='Dr6ap'><dir id='Dr6ap'><q id='Dr6ap'></q></dir></style></legend>

        <small id='Dr6ap'></small><noframes id='Dr6ap'>

        执行 JpaTest 时找不到 @SpringBootConfiguration

        Unable to find a @SpringBootConfiguration when doing a JpaTest(执行 JpaTest 时找不到 @SpringBootConfiguration)
            <bdo id='1ePMZ'></bdo><ul id='1ePMZ'></ul>
            • <small id='1ePMZ'></small><noframes id='1ePMZ'>

                    <tbody id='1ePMZ'></tbody>
                1. <legend id='1ePMZ'><style id='1ePMZ'><dir id='1ePMZ'><q id='1ePMZ'></q></dir></style></legend>
                  <i id='1ePMZ'><tr id='1ePMZ'><dt id='1ePMZ'><q id='1ePMZ'><span id='1ePMZ'><b id='1ePMZ'><form id='1ePMZ'><ins id='1ePMZ'></ins><ul id='1ePMZ'></ul><sub id='1ePMZ'></sub></form><legend id='1ePMZ'></legend><bdo id='1ePMZ'><pre id='1ePMZ'><center id='1ePMZ'></center></pre></bdo></b><th id='1ePMZ'></th></span></q></dt></tr></i><div id='1ePMZ'><tfoot id='1ePMZ'></tfoot><dl id='1ePMZ'><fieldset id='1ePMZ'></fieldset></dl></div>
                  <tfoot id='1ePMZ'></tfoot>
                2. 本文介绍了执行 JpaTest 时找不到 @SpringBootConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是框架新手(刚刚通过了课程),这是我第一次使用 Spring Boot.

                  I'm new to frameworks (just passed the class) and this is my first time using Spring Boot.

                  我正在尝试运行一个简单的 Junit 测试以查看我的 CrudRepositories 是否确实在工作.

                  I'm trying to run a simple Junit test to see if my CrudRepositories are indeed working.

                  我不断收到的错误是:

                  无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)java.lang.IllegalStateException

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

                  Spring Boot 不会自己配置吗?

                  Doesn't Spring Boot configure itself?

                  我的测试班:

                  @RunWith(SpringRunner.class)
                  @DataJpaTest
                  @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
                  public class JpaTest {
                  
                  @Autowired
                  private AccountRepository repository;
                  
                  @After
                  public void clearDb(){
                      repository.deleteAll();
                  }
                  
                   @Test
                   public void createAccount(){
                       long id = 12;
                       Account u = new Account(id,"Tim Viz");
                       repository.save(u);
                  
                       assertEquals(repository.findOne(id),u);
                  
                   }
                  
                  
                   @Test
                   public void findAccountByUsername(){
                       long id = 12;
                       String username = "Tim Viz";
                       Account u = new Account(id,username);
                       repository.save(u);
                  
                       assertEquals(repository.findByUsername(username),u);
                  
                   }
                  

                  我的 Spring Boot 应用启动器:

                  My Spring Boot application starter:

                  @SpringBootApplication
                  @EnableJpaRepositories(basePackages = {"domain.repositories"})
                  @ComponentScan(basePackages = {"controllers","domain"})
                  @EnableWebMvc
                  @PropertySources(value    {@PropertySource("classpath:application.properties")})
                      @EntityScan(basePackages={"domain"})
                      public class Application extends SpringBootServletInitializer {
                          public static void main(String[] args) {
                              ApplicationContext ctx = SpringApplication.run(Application.class, args);         
                  
                          }
                      }
                  

                  我的仓库:

                  public interface AccountRepository extends CrudRepository<Account,Long> {
                  
                      public Account findByUsername(String username);
                  
                      }
                  }
                  

                  推荐答案

                  确实,Spring Boot 确实在很大程度上设置了自己.您可能已经删除了很多您发布的代码,尤其是在 Application 中.

                  Indeed, Spring Boot does set itself up for the most part. You can probably already get rid of a lot of the code you posted, especially in Application.

                  我希望你已经包含了所有类的包名,或者至少包含 ApplicationJpaTest 的包名.关于 @DataJpaTest 和其他一些注解的事情是他们在当前包中寻找 @SpringBootConfiguration 注解,如果在那里找不到它,他们会遍历包直到他们找到它为止.

                  I wish you had included the package names of all your classes, or at least the ones for Application and JpaTest. The thing about @DataJpaTest and a few other annotations is that they look for a @SpringBootConfiguration annotation in the current package, and if they cannot find it there, they traverse the package hierarchy until they find it.

                  例如,如果您的测试类的完全限定名称是 com.example.test.JpaTest 而您的应用程序的名称是 com.example.Application,那么您的测试类将能够找到 @SpringBootApplication(以及其中的 @SpringBootConfiguration).

                  For example, if the fully qualified name for your test class was com.example.test.JpaTest and the one for your application was com.example.Application, then your test class would be able to find the @SpringBootApplication (and therein, the @SpringBootConfiguration).

                  但是,如果应用程序位于包层次结构的不同分支中,例如 com.example.application.Application,它将找到它.

                  If the application resided in a different branch of the package hierarchy, however, like com.example.application.Application, it would not find it.

                  考虑以下 Maven 项目:

                  Consider the following Maven project:

                  my-test-project
                    +--pom.xml
                    +--src
                      +--main
                        +--com
                          +--example
                            +--Application.java
                      +--test
                        +--com
                          +--example
                            +--test
                              +--JpaTest.java
                  

                  然后是Application.java中的如下内容:

                  package com.example;
                  
                  @SpringBootApplication
                  public class Application {
                  
                      public static void main(String[] args) {
                          SpringApplication.run(Application.class, args);
                      }
                  }
                  

                  后面是JpaTest.java的内容:

                  package com.example.test;
                  
                  @RunWith(SpringRunner.class)
                  @DataJpaTest
                  public class JpaTest {
                  
                      @Test
                      public void testDummy() {
                      }
                  }
                  

                  一切都应该正常工作.如果您在 src/main/com/example 中创建一个名为 app 的新文件夹,然后将您的 Application.java 放入其中(并更新package 文件中的声明),运行测试会给你以下错误:

                  Everything should be working. If you create a new folder inside src/main/com/example called app, and then put your Application.java inside it (and update the package declaration inside the file), running the test will give you the following error:

                  java.lang.IllegalStateException:无法找到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)

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

                  这篇关于执行 JpaTest 时找不到 @SpringBootConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                      <small id='KRQ50'></small><noframes id='KRQ50'>

                        <tbody id='KRQ50'></tbody>
                      <i id='KRQ50'><tr id='KRQ50'><dt id='KRQ50'><q id='KRQ50'><span id='KRQ50'><b id='KRQ50'><form id='KRQ50'><ins id='KRQ50'></ins><ul id='KRQ50'></ul><sub id='KRQ50'></sub></form><legend id='KRQ50'></legend><bdo id='KRQ50'><pre id='KRQ50'><center id='KRQ50'></center></pre></bdo></b><th id='KRQ50'></th></span></q></dt></tr></i><div id='KRQ50'><tfoot id='KRQ50'></tfoot><dl id='KRQ50'><fieldset id='KRQ50'></fieldset></dl></div>
                      • <bdo id='KRQ50'></bdo><ul id='KRQ50'></ul>
                        <legend id='KRQ50'><style id='KRQ50'><dir id='KRQ50'><q id='KRQ50'></q></dir></style></legend>
                          1. <tfoot id='KRQ50'></tfoot>