集成测试和 build.gradle Java Spring。

huangapple go评论76阅读模式
英文:

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

此外,关于testRuntimetestImplementation和其他依赖类型的问题,您可以查阅官方文档或相关教程以获得更多详细信息。

英文:

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 &quot;http://localhost:&quot; + webServerPort + uri;
}
@Test
public void testSetNewAppUser() {
HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(null,null);
ResponseEntity&lt;String&gt; response =
restTemplate.exchange(
createURLWithPort(&quot;/{RobsAndroid}/setNewAppUser/{Rob}&quot;),
HttpMethod.GET,
entity,
String.class);
String expected = &quot;New AppUser: &quot; + &quot;RobsAndroid&quot;+ &quot; set OK.&quot;;
assertEquals(expected,response.getBody());
assertEquals(HttpStatus.OK,response.getStatusCode());
}
}

However, I am getting an error:

Execution failed for task &#39;:integrationTest&#39;.
&gt; 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 &#39;org.springframework.boot&#39; version &#39;2.2.5.RELEASE&#39;
id &#39;io.spring.dependency-management&#39; version &#39;1.0.9.RELEASE&#39;
id &#39;java&#39;
}
group = &#39;com.socialinteraction&#39;
version = &#39;0.0.1-SNAPSHOT&#39;
sourceCompatibility = &#39;1.8&#39;
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
sourceSets {
integrationTest {
java {
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
srcDir file(&#39;src/integration-test/java&#39;)
}
resources.srcDir file(&#39;src/integration-test/resources&#39;)
}
}
configurations {
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
integrationTestImplementation.extendsFrom testImplementation
}
dependencies {
implementation &#39;org.springframework.boot:spring-boot-starter-data-mongodb&#39;
implementation &#39;org.springframework.boot:spring-boot-starter-web&#39;
compileOnly &#39;org.projectlombok:lombok&#39;
annotationProcessor &#39;org.projectlombok:lombok&#39;
testImplementation(&#39;org.springframework.boot:spring-boot-starter-test&#39;) {
exclude group: &#39;org.junit.vintage&#39;, module: &#39;junit-vintage-engine&#39;
}
testImplementation(&#39;org.junit.jupiter:junit-jupiter-api:5.4.2&#39;)
testRuntime(&#39;org.junit.jupiter:junit-jupiter-engine:5.4.2&#39;)
//    testCompile &quot;org.powermock:powermock-module-junit4:1.6.4&quot;
//    testCompile &#39;org.mockito:mockito-core:2.7.22&#39;
implementation &#39;org.jetbrains:annotations:15.0&#39;
implementation &#39;junit:junit:4.12&#39;
implementation &#39;org.testng:testng:6.9.6&#39;
implementation &#39;junit:junit:4.12&#39;
implementation &#39;junit:junit:4.12&#39;
implementation &#39;org.testng:testng:6.9.6&#39;
}
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.

huangapple
  • 本文由 发表于 2020年4月4日 01:24:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61017234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定