[英]Integration test and build.gradle Java Spring
我正在嘗試為我的服務編寫集成測試。
我試圖寫一個簡單的測試作為我的第一個測試。 我的 WebControllerIT class 如下:
@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.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
}
這個對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.