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

问题描述 投票:0回答:1

我正在尝试编写我的第一个集成测试。 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:当我尝试运行测试类时,没有可运行的方法,这是我创建的新integrationTest Gradle任务的测试方法。

No runnable methods exception

这是我的上下文包结构:

Project structure

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

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

java spring testing integration-testing
1个回答
0
投票

我不确定,但可能您正在将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

© www.soinside.com 2019 - 2024. All rights reserved.