找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)
Posted
技术标签:
【中文标题】找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)【英文标题】:Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test 【发布时间】:2018-05-09 07:33:34 【问题描述】:我正在使用Spring Data JPA
和Spring Boot
。应用的布局是这样的
main
+-- java
+-- com/lapots/game/monolith
+-- repository/relational
+--RelationalPlayerRepository.java
+-- web
+--GrandJourneyMonolithApplication.java
+-- config
+-- RelationalDBConfiguration.java
test
+-- java
+-- com/lapots/game/monolith
+-- repository/relational
+-- RelationalPlayerRepositoryTests.java
+-- web
+-- GrandJourneyMonolithApplicationTests.java
我的对象的存储库如下所示
public interface RelationalPlayerRepository extends JpaRepository<Player, Long>
另外对于我提供的存储库配置
@Configuration
@EnableJpaRepositories(basePackages = "com.lapots.game.monolith.repository.relational")
@EntityScan("com.lapots.game.monolith.domain")
public class RelationalDBConfiguration
我的主应用是这样的
@SpringBootApplication
@ComponentScan("com.lapots.game.monolith")
public class GrandJourneyMonolithApplication
@Autowired
private RelationalPlayerRepository relationalPlayerRepository;
public static void main(String[] args)
SpringApplication.run(GrandJourneyMonolithApplication.class, args);
@Bean
public CommandLineRunner initPlayers()
return (args) ->
Player p = new Player();
p.setLevel(10);
p.setName("Master1909");
p.setClazz("warrior");
relationalPlayerRepository.save(p);
;
应用程序测试如下所示
@RunWith(SpringRunner.class)
@SpringBootTest
public class GrandJourneyMonolithApplicationTests
@Test
public void contextLoads()
存储库的测试如下所示
@RunWith(SpringRunner.class)
@DataJpaTest
public class RelationalPlayerRepositoryTests
@Autowired
private TestEntityManager entityManager;
@Autowired
private RelationalPlayerRepository repository;
@Test
public void testBasic()
Player expected = createPlayer("Master12", "warrior", 10);
this.entityManager.persist(expected);
List<Player> players = repository.findAll();
assertThat(repository.findAll()).isNotEmpty();
assertEquals(expected, players.get(0));
private Player createPlayer(String name, String clazz, int level)
Player p = new Player();
p.setId(1L);
p.setName(name);
p.setClazz(clazz);
p.setLevel(level);
return p;
但是当我尝试运行测试时,我得到了错误
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.041 sec <<< FAILURE! - in com.lapots.game.monolith.repository.relational.RelationalPlayerRepositoryTests
initializationError(com.lapots.game.monolith.repository.relational.RelationalPlayerRepositoryTests) Time elapsed: 0.006 sec <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
at org.springframework.util.Assert.state(Assert.java:70)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:137)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:82)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
有什么问题?
域Player
看起来像这样
@Data
@Entity
@Table(schema = "app", name = "players")
public class Player
@Id
@GeneratedValue
private Long id;
@Transient
Set<Player> comrades;
@Column(unique = true)
private String name;
private int level;
@Column(name = "class")
private String clazz;
【问题讨论】:
如果你用@ContextConfiguration(classes = GrandJourneyMonolithApplication.class)
注释RelationalPlayerRepositoryTests
会发生什么?
@SamBrannen 它显示错误failed to replace DataSource with embedded
【参考方案1】:
删除文件 module-info.java。为我工作。
【讨论】:
【参考方案2】:在我的情况下,这是由于某些 [move|copy/past] 类造成的。对某些人来说,package
子句要么[未正确更新|不存在|正确,但不知何故未采纳更改]。
无论如何,请检查您的项目包装。
【讨论】:
【参考方案3】:只有在 @SpringBootTest 类中同时包含上下文配置类和应用程序类后,我才能解决此问题。
【讨论】:
【参考方案4】:src/test/java
包和src/main/java
包应该匹配。
我有同样的问题,
src/main/java
软件包以 com.comp.example 开头,但
src/test/java
包以 com.sample.example 开头
因为这个 Spring Boot 应用程序无法获取应用程序的配置,它从 @SpringBootApplication
类获取。
所以测试用例应该属于src/main/java
中的@SpringBootApplication
所写的包。
【讨论】:
我正在使用带有简单 REST 控制器的 Spring Boot。没有使用任何 JPA 并且得到与我的控制器测试标题中指定的相同的错误。此外,我正在为我的 Spring Boot 应用程序使用自定义配置。这个答案(2018 年 10 月 19 日 7:20 Saurabh Parmar)有帮助。对我来说,根本原因也是包名。我在 src/test/java 下的测试包与 src/main/java 下的包不匹配。一旦我修复它就可以了。 太棒了? 我的测试包名有错别字 我完全忘记添加包名了。成功了 为了避免将测试类包与生产代码包匹配,只需在 SpringBootTest 注释中指定用于加载应用程序上下文的类,如 @SpringBootTest(classes = Application.class) 【参考方案5】:@SpringBootTest
有一个名为classes
的参数。它接受一组用于配置的类。将配置文件的类添加到其中,例如:
@SpringBootTest(classes=com.lapots.game.monolith.web.GrandJourneyMonolithApplication.class)
【讨论】:
这对我有用:@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
- 见***.com/questions/43468754/…【参考方案6】:
Spring Boot 启动时,会从项目的顶部到底部扫描类路径以查找配置文件。您的配置在另一个文件下,这是问题的原因。将你的配置移到 monolith 包,一切都会好起来的。
【讨论】:
但是不应该在测试中指定配置类吗? 我也移动了configuration
班级,但没有帮助
上面的答案应该是正确的。将您的应用程序构建为suggested in the documentation,测试应该能够向上搜索以找到配置。由于禁用自动配置的注释,您可能会遇到其他问题。尝试同时删除 @EnableJpaRepositories
、@EntityScan
和 @ComponentScan
。以上是关于找不到@SpringBootConfiguration,您需要在测试中使用@ContextConfiguration 或@SpringBootTest(classes=...)的主要内容,如果未能解决你的问题,请参考以下文章