java - 集成测试和 build.gradle Java Spring
问题描述
我正在尝试为我的服务编写集成测试。
我试图写一个简单的测试作为我的第一个测试。我的 WebControllerIT 类如下:
@RunWith(SpringRunner.class)
@SpringBootTest(
classes = SocialInteractionApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebControllerIT {
@LocalServerPort private int webServerPort;
@Autowired private AppUserRepository AppUserRepository;
@Autowired private WebController webController;
private RestTemplate restTemplate = new RestTemplate();
private String createURLWithPort(String uri) {
return "http://localhost:" + webServerPort + uri;
}
@Test
public void testSetNewAppUser() {
HttpEntity<String> entity = new HttpEntity<String>(null,null);
ResponseEntity<String> response =
restTemplate.exchange(
createURLWithPort("/{RobsAndroid}/setNewAppUser/{Rob}"),
HttpMethod.GET,
entity,
String.class);
String expected = "New AppUser: " + "RobsAndroid"+ " set OK.";
assertEquals(expected,response.getBody());
assertEquals(HttpStatus.OK,response.getStatusCode());
}
}
但是,我收到一个错误:
Execution failed for task ':integrationTest'.
> No tests found for given includes: [com.socialinteraction.componenttests.WebControllerIT.testSetNewAppUser](filter.includeTestsMatching)
我似乎无法弄清楚发生了什么,有什么想法吗?
为了提供更多背景信息,我不确定如何从组件测试开始。这包括:它们应该位于哪里,它们应该具有哪些依赖项,如何在 gradle build 中运行它们等。我遵循了一个教程,我的 build.gradle 文件最终如下:
plugins {
id 'org.springframework.boot' version '2.2.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.socialinteraction'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file('src/integration-test/java')
}
resources.srcDir file('src/integration-test/resources')
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
integrationTestImplementation.extendsFrom testImplementation
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation('org.junit.jupiter:junit-jupiter-api:5.4.2')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
// testCompile "org.powermock:powermock-module-junit4:1.6.4"
// testCompile 'org.mockito:mockito-core:2.7.22'
implementation 'org.jetbrains:annotations:15.0'
implementation 'junit:junit:4.12'
implementation 'org.testng:testng:6.9.6'
implementation 'junit:junit:4.12'
implementation 'junit:junit:4.12'
implementation 'org.testng:testng:6.9.6'
}
test {
useJUnitPlatform()
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
check.dependsOn integrationTest
integrationTest.mustRunAfter test
我基本上确保集成测试依赖项是从单元测试继承的,有一个新的 intergrationTest 类型,并且它是在单元测试之后的构建中运行的。
附带问题 - 不确定 testRuntime、testImplementation 和其他依赖类型是否都需要/正确,所以如果有人有一个很好的链接来解释这些会很棒吗?
非常感谢你的帮助!
解决方案
尝试useJUnitPlatform()
在您的任务描述中添加:
task integrationTest(type: Test) {
useJUnitPlatform()
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
这个对我有用。
推荐阅读
- javascript - 自定义样式安装在 Angular 应用程序中的包的最佳方式
- installation - 如何使用 wix 安装程序添加公司名称
- java - 使用 MapStruct 时存在多个构造函数时出现“发现不明确的构造函数”错误
- c++ - 警告:支持指针的对象将在 std::pair 的完整表达式结束时被销毁
- java - 在 Android 中单击按钮播放声音给出空指针异常
- r - 同时向多个数据表(帧)添加多列
- javascript - 如何以角度创建六边形进度条
- c# - 如何使用 IEnumerator,因为我有一些错误
- laravel - 保存的日期未显示在表单中
- code-coverage - Codecov 按覆盖率排序