繁体   English   中英

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

[英]Java Integration Test No Runnable Methods error - Spring Runner

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

这是我的 WebControllerIT.java 这是我的 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);
}
}

我正在尝试解决为什么我的集成测试不起作用,因此使用了简单的 assertTrue() 方法。

我目前得到一个 java.lang.Exception: No runnable methods,当我尝试运行测试 class 时,我创建的新 integrationTest Gradle 任务的测试方法。

没有可运行方法异常

这是我的 package 结构的上下文:

项目结构

最后,这是我的 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

任何想法为什么我不能运行我的集成测试? 任何输入都会有所帮助,谢谢!

我不确定,但可能您正在将 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、测试依赖项

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM