在执行JpaTest时无法找到@SpringBootConfiguration [英] Unable to find a @SpringBootConfiguration when doing a JpaTest
问题描述
我是框架的新手(刚刚通过了这个类),这是我第一次使用springboot。
I'm new to frameworks (just passed the class) and this is my first time using springboot.
我正在尝试运行简单的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
没有弹簧启动配置本身?
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启动应用程序启动器
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确实在很大程度上自我设定。你可能已经摆脱了很多你发布的代码,特别是在应用程序
。
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
.
我希望你已包含所有类的包名,或至少包含应用程序
和 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
的新文件夹,然后把你的<$ c其中有$ c> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!