0

Видел уже подобный вопрос и ответ на него, но мне хочется узнать, что мне нужно ещё изменить, чтобы избавиться от ошибки? Что я такого подключил, что он постоянно это выводит? Как можно в IDEA изменить, что использую Junit 5? Но когда делаю в другом классе, с такими же аннотациями, но методы отрабатывают корректно, то никакие ошибки не выпадают.

import org.junit.Before;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.junit4.SpringRunner;
import task.homerent.dto.ContractDto;
import task.homerent.dto.HouseDto;
import task.homerent.dto.UserDto;
import task.homerent.model.Contract;
import task.homerent.model.House;
import task.homerent.model.User;
import task.homerent.repository.ContractRepository;
import task.homerent.repository.HouseRepository;
import task.homerent.repository.UserRepository;
import task.homerent.service.ContractService;
import task.homerent.service.HouseService;
import task.homerent.service.UserService;
import task.homerent.web.HouseRestController;

import java.time.LocalDate;
import java.util.Collection;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class HouseTest {

    @Autowired
    private UserService userService;
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private HouseService houseService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private HouseRestController houseRestController;
    @Autowired
    private HouseRepository houseRepository;

    @MockBean
    private AuthController authController;

    @BeforeEach
    public void AuthLogin() throws Exception {
        UserDto userDto = new UserDto();
        userDto.setEmail("[email protected]");
        userDto.setPassword("igor");
        authController.getLoginPage(userDto);
    }

    @Test
    void ContractHouse() {
        LocalDate endDate = LocalDate.of(2020, 10, 31);
        LocalDate startDate = LocalDate.of(2020, 10, 20);
        ContractDto contractDto = new ContractDto();
        contractDto.setEndDate(endDate);
        contractDto.setStartDate(startDate);
        contractDto.setHouseId((long)5);
        contractDto.setTenantId((long)5);
        String freeHouse = houseRestController.findAllByDate(contractDto);
        assertThat(freeHouse).isNotNull();
        assertThat(freeHouse).isEqualTo("Квартира занята");
    }

    @Test
    void ListHouse() {
        List<House> freeHouse = houseService.findfreehouse();
        assertThat(freeHouse).isNotNull();
    }
}

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.proselyte</groupId>
    <artifactId>springsecuritydemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringSecurityDemo</name>
    <description>This is Spring Security course project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <java.version>11</java.version>
        <mysql.connector.version>8.0.21</mysql.connector.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.20.Final</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.16</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.6.3</version>
                <configuration>
                    <propertyFile>${project.basedir}/src/main/resources/liquibase.properties</propertyFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
1
  • Не знаю каким образом, но проблема перестала появляться, абсолютно ничего не менял, кроме как аннотацию @RunWith(SpringRunner.class) заменил на @ExtendWith(SpringExtension.class)
    – Blacit
    7 окт 2020 в 8:38

1 ответ 1

0

У меня оказался такой вариант ответа правильным:

Заменить над классом с тестом. Я не до конца разобрался, что необходимо в скобочках указываться. Но такой вариант заработал, взял отсюда.

@RunWith(SpringRunner.class) 

На

@ExtendWith(SpringExtension.class)

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.