执行 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
相关文章