这是我的
build.gradle
文件:
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.4'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.kriegzeug'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.vintage:junit-vintage-engine:5.8.2'
}
tasks.named('test') {
useJUnitPlatform()
}
这是我要运行的简单测试。我要做的就是确定我的代码正确地将 MongoDB 文档转换为我的自定义 java 对象:
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataModelTests {
@Autowired
CardRepository cardRepository;
@Test
public void SoloWithNoAttacks() {
// GIVEN
String cardName = "Mortitheurge Willbreaker";
// WHEN
Card card = cardRepository.findCardByName(cardName);
// THEN
Assertions.assertEquals("Mortitheurge Willbreaker", card.getName());
Assertions.assertEquals(3, card.getEdition());
Assertions.assertEquals("2021 v1", card.getRevision());
Assertions.assertEquals(List.of("Skorne", "Solo"), card.getKeywords());
}
}
这是我尝试运行此测试时遇到的错误:
14:14:10.538 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [DataModelTests]: using SpringBootContextLoader
14:14:10.542 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.kriegzeug.college.DataModelTests]: no resource found for suffixes {-context.xml, Context.groovy}.
14:14:10.542 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.kriegzeug.college.DataModelTests]: DataModelTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
14:14:10.553 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using ContextCustomizers for test class [DataModelTests]: [DisableObservabilityContextCustomizer, PropertyMappingContextCustomizer, Customizer, ExcludeFilterContextCustomizer, DuplicateJsonObjectContextCustomizer, MockitoContextCustomizer, TestRestTemplateContextCustomizer]
我过去构建 SpringBoot 测试套件没有问题,这是一个新项目,也是我有一段时间第一次接触 SprintBoot。我只是不明白我在设置中做错了什么。
我遇到了同样的错误,但我正在使用 maven。在我的例子中,测试类在其他一些 pkg 中,我将它保存在存储库 pkg 中。然后这个错误就消失了。
在你的情况下,请尽量在 cardRepository 的 pkg 中保留“DataModelTests”。