Unable to find a @SpringBootConfiguration when doing a JpaTest(执行 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
.
我希望你已经包含了所有类的包名,或者至少包含 Application
和 JpaTest
的包名.关于 @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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:执行 JpaTest 时找不到 @SpringBootConfiguration
基础教程推荐
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01