簡體   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