6
respostas

Erro ao executar Junit

Professor, ao executar o teste abaixo:

@Test void deveriaCarregarUmCursoAoBuscarPeloSeuNome() {

    String nomeCurso = "HTML 5";

    Curso curso = repository.findByNome(nomeCurso);

    Assert.assertNotNull(curso);
    Assert.assertEquals(nomeCurso, curso.getNome());
}

O Junit não executa dando o erro a seguir: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner at localhost:58890<terminated, exit value: 0>C:\Program Files\Java\jdk1.8.0_271\bin\javaw.exe (13 de nov. de 2021 14:29:16)

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

6 respostas

Resolvido, tem relação com o import abaixo

import org.junit.jupiter.api.Test;

Obrigado.

Professor os testes individualmente funcionam, mas agora esta dando erro nos asserts ao dar mvn clean package no projeto.

[INFO] [1;31mBUILD FAILURE[m [INFO] [1m------------------------------------------------------------------------[m [INFO] Total time: 6.739 s [INFO] Finished at: 2021-11-14T13:46:01-03:00 [INFO] [1m------------------------------------------------------------------------[m [ERROR] Failed to execute goal [32morg.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile[m [1m(default-testCompile)[m on project [36mforum[m: [1;31mCompilation failure[m: Compilation failure: [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/repository/CursoRepositoryTest.java:[3,17] package org.junit does not exist [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/repository/CursoRepositoryTest.java:[5,24] package org.junit.runner does not exist [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/repository/CursoRepositoryTest.java:[15,2] cannot find symbol [ERROR] symbol: class RunWith [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/controller/AutenticacaoControllerTest.java:[6,24] package org.junit.runner does not exist [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/controller/AutenticacaoControllerTest.java:[17,2] cannot find symbol [ERROR] symbol: class RunWith [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/ForumApplicationTests.java:[3,17] cannot find symbol [ERROR] symbol: class Assert [ERROR] location: package org.junit [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/ForumApplicationTests.java:[4,17] cannot find symbol [ERROR] symbol: class Test [ERROR] location: package org.junit [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/ForumApplicationTests.java:[5,24] package org.junit.runner does not exist [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/ForumApplicationTests.java:[9,2] cannot find symbol [ERROR] symbol: class RunWith [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/ForumApplicationTests.java:[13,10] cannot find symbol [ERROR] symbol: class Test [ERROR] location: class br.com.alura.forum.ForumApplicationTests [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/repository/CursoRepositoryTest.java:[37,17] cannot find symbol [ERROR] symbol: variable Assert [ERROR] location: class br.com.alura.forum.repository.CursoRepositoryTest [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/repository/CursoRepositoryTest.java:[38,17] cannot find symbol [ERROR] symbol: variable Assert [ERROR] location: class br.com.alura.forum.repository.CursoRepositoryTest [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/repository/CursoRepositoryTest.java:[45,17] cannot find symbol [ERROR] symbol: variable Assert [ERROR] location: class br.com.alura.forum.repository.CursoRepositoryTest [ERROR] /C:/Users/wagner.silva/Documents/alura/forum/src/test/java/br/com/alura/forum/ForumApplicationTests.java:[15,17] cannot find symbol [ERROR] symbol: variable Assert [ERROR] location: class br.com.alura.forum.ForumApplicationTests [ERROR] -> [1m[Help 1]

Oi Wagner,

Parece ser problema da versão do JUnit mesmo.

Posta aqui o seu arquivo pom.xml.

4.0.0

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.0-SNAPSHOT</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>br.com.alura</groupId>
<artifactId>forum</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>forum</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <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-cache</artifactId>
    </dependency>

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

    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.1</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

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

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.13.Final</version>
    </dependency>

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

    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <version>2.4.3</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctor-maven-plugin</artifactId>
            <version>1.5.8</version>
            <executions>
                <execution>
                    <id>generate-docs</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>process-asciidoc</goal>
                    </goals>
                    <configuration>
                        <backend>html</backend>
                        <doctype>book</doctype>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.restdocs</groupId>
                    <artifactId>spring-restdocs-asciidoctor</artifactId>
                    <version>${spring-restdocs.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
    <pluginRepository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

Oi Wagner,

Pode ser porque você está utilizando a versão 2.6.0 do Spring Boot que ainda está como SNAPSHOT. A recomendação é utilizar a última versão estável: 2.5.6.

Além disso, os imports do JUnit devem vir do pacote org.junit.jupiter.api, que é da versão 5.

Obrigado Rodrigo.