我使用的是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;
}
8条答案
按热度按时间deikduxw1#
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
的相同包。anauzrmj2#
当Sping Boot 启动时,它会从项目的顶部到底部扫描类路径,以找到配置文件。您的配置文件位于另一个文件下,这就是问题的原因之一。将您的配置文件移至 monolith 包的上部,一切都会好起来。
irlmq6kh3#
@SpringBootTest
有一个名为classes
的参数。它接受用于配置的类数组。请将配置文件的类添加到该数组中,例如:vaqhlq814#
测试
src/test/java
文件也应遵循与src/main/java
中相同的目录结构。cczfrluj5#
在我的情况下,这是由于一些[移动|复制/粘贴]。对于某些类,
package
子句可能[未正确更新|not present],而且除了IDE之外,并未取得变更。无论如何,请检查您的项目 Package 。
iibxawm46#
删除文件
module-info.java
。这对我很有效。oalqel3c7#
只有在
@SpringBootTest
类中同时包含上下文配置类和应用程序类之后,我才能解决这个问题。wkyowqbh8#
如果您的项目中没有可测试的代码,并且您在Sping Boot 默认测试类中有默认测试块
删除测试注解和contextLoads()方法。