Java Интеграционный тест без ошибок Runnable Methods - Spring Runner - PullRequest
0 голосов
/ 06 апреля 2020

Я пытаюсь написать свой первый интеграционный тест. Тесты 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: при запуске тестового класса нет запускаемых методов, этот тестовый метод нового интеграционного теста Созданная мной задача 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

Есть идеи, почему я не могу запустить свои интеграционные тесты? Любой вклад будет полезен, спасибо!

1 Ответ

0 голосов
/ 06 апреля 2020

Я не уверен, но, вероятно, вы смешиваете 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 в этом случае не требуется.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...