英文:
Integration test and 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.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
此外,关于testRuntime
、testImplementation
和其他依赖类型的问题,您可以查阅官方文档或相关教程以获得更多详细信息。
英文:
I am trying to write integration tests for my service.
I have tried to write a simple test as my first test. My WebControllerIT class is as follows:
@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());
}
}
However, I am getting an error:
Execution failed for task ':integrationTest'.
> No tests found for given includes: [com.socialinteraction.componenttests.WebControllerIT.testSetNewAppUser](filter.includeTestsMatching)
I can't seem to work out what's going on, any ideas?
To give some more context, I wasn't sure how to start with component testing. This included: where they should be located, what dependencies they should have, how to run them in the gradle build etc. I followed a tutorial and my build.gradle file has ended up as follows:
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
I essentially made sure the integration test dependencies were inherited from unit tests, there was a new intergrationTest type and that this was ran in the build after unit tests.
Side question - not sure testRuntime, testImplementation and other dependencies type are all needed/right, so a good link to explain these would be great if someone has one?
Thanks so much for your help!
答案1
得分: 2
尝试在任务描述中添加useJUnitPlatform()
:
task integrationTest(type: Test) {
useJUnitPlatform()
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
它对我有效。
英文:
try to add useJUnitPlatform()
inside your task desc:
task integrationTest(type: Test) {
useJUnitPlatform()
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
It works for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论