Java集成测试无可运行的方法错误 – Spring Runner

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

Java Integration Test No Runnable Methods error - Spring Runner

问题

以下是您提供的代码的翻译:

我正试图编写我的第一个集成测试JUnit 测试正常工作

这是我的 WebControllerIT.java它是我的 WebController 类的对应集成测试

    package com.socialinteraction.componenttests;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    import static org.junit.jupiter.api.Assertions.assertTrue;
    
    import com.socialinteraction.SocialInteractionApplication;
    import com.socialinteraction.WebController;
    import com.socialinteraction.database.repositories.AppUserRepository;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.web.server.LocalServerPort;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.web.client.RestTemplate;
    
    @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() {
    
        assertTrue(true);
        assertTrue(false);
    }
    }

我正尝试解决我的集成测试为什么无法工作的问题因此使用了简单的 assertTrue() 方法

当我尝试运行测试类时我目前收到一个 java.lang.Exception: No runnable methods 的错误消息这是在我尝试运行新创建的 integrationTest Gradle 任务的测试方法时出现的

[![No runnable methods exception][1]][1]

这是我项目的包结构以供参考

[![Project structure][2]][2]

最后这是我的 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
        }
        integrationTestCompile.extendsFrom testCompile
        integrationTestRuntime.extendsFrom testRuntime
        integrationTestImplementation.extendsFrom testImplementation
    }
    
    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')
        }
    }
    
    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'
        integrationTestCompile 'org.assertj:assertj-core:3.0.0'
    }
    
    test {
        useJUnitPlatform()
    }
    
    task integrationTest(type: Test) {
    
        testClassesDirs = sourceSets.integrationTest.output.classesDirs
        classpath = sourceSets.integrationTest.runtimeClasspath
    }
    
    check.dependsOn integrationTest
    integrationTest.mustRunAfter test

有什么想法为什么我无法运行我的集成测试任何意见都将是有帮助的谢谢

  [1]: https://i.stack.imgur.com/JxYYT.png
  [2]: https://i.stack.imgur.com/cynNg.png

请注意,这里提供的翻译只是您提供的原始文本的翻译部分,不包括其他内容。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

I am trying to write my first integration test. JUnit tests are working fine.

Here is my WebControllerIT.java which is the corresponding integration test for my WebController class:

package com.socialinteraction.componenttests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.socialinteraction.SocialInteractionApplication;
import com.socialinteraction.WebController;
import com.socialinteraction.database.repositories.AppUserRepository;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
@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() {
assertTrue(true);
assertTrue(false);
}
}

I am trying to troubleshoot why my Integration Test is not working, hence the simple assertTrue() methods.

I am currently getting a java.lang.Exception: No runnable methods, when I try and run the test class, that test method of the new integrationTest Gradle task I created.

Java集成测试无可运行的方法错误 – Spring Runner

Here is my package structure for context:

Java集成测试无可运行的方法错误 – Spring Runner

Lastly, here is my build.gradle file:

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
}
integrationTestCompile.extendsFrom testCompile
integrationTestRuntime.extendsFrom testRuntime
integrationTestImplementation.extendsFrom testImplementation
}
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')
}
}
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'
integrationTestCompile 'org.assertj:assertj-core:3.0.0'
}
test {
useJUnitPlatform()
}
task integrationTest(type: Test) {
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
}
check.dependsOn integrationTest
integrationTest.mustRunAfter test

Any ideas why I cannot run my integration tests? Any input would be helpful, thank you!

答案1

得分: 1

我不确定,但可能你在混淆junit4和junit5。尝试使用这些依赖项:

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.junit.jupiter:junit-jupiter-api'

然后移除所有的junit、assertJ和testing依赖项。

然后将 @RunWith(SpringRunner.class) 替换为 @ExtendWith(SpringExtension.class)

而在这种情况下,classes = SocialInteractionApplication.class 可能是不需要的。

英文:

I'm not sure but probably you are mixing junit4 with junit5. Try using this dependencies:

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.junit.jupiter:junit-jupiter-api'

And remove all junit, assertJ, testing dependencies

Then replace @RunWith(SpringRunner.class) with @ExtendWith(SpringExtension.class)

And classes = SocialInteractionApplication.class is probably not needed in that case.

huangapple
  • 本文由 发表于 2020年4月6日 22:11:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61061789.html
匿名

发表评论

匿名网友

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

确定