No tests found for given includes: [CosServiceTest](--tests filter)
Runner-ul JUnit 5 nu a găsit niciun test, de obicei pentru că ai importat org.junit.Test (JUnit 4) în loc de org.junit.jupiter.api.Test. Platforma JUnit 5 ignoră adnotarea veche. Schimbă importul (și aserțiunile) pe pachetul org.junit.jupiter.api.
Explicat de Tudor Andrei Dumitrescu, Senior Software Developer, fondatorul Academiei ProgramatorilorActualizat la
Ce înseamnă
Spring Boot 3 folosește JUnit 5 (Jupiter). Motorul Jupiter recunoaște doar @Test din org.junit.jupiter.api. Dacă IDE-ul a ales automat import org.junit.Test; (JUnit 4, venit ca dependență indirectă) metodele nu mai sunt teste pentru Jupiter, iar fără motorul „vintage” nimeni nu le rulează. Gradle raportează No tests found for given includes: [CosServiceTest](--tests filter), Maven Tests run: 0, iar IntelliJ No tests were found.
Alte cauze ale aceluiași simptom: metode de test private sau static, clasa de test numită fără sufixul Test/Tests (Maven Surefire caută implicit *Test), testul pus în src/main/java în loc de src/test/java, ori lipsa useJUnitPlatform() în build.gradle.
Pericolul e că build-ul trece verde: zero teste rulate înseamnă zero teste picate.
De ce apare: cauzele frecvente
1Import din JUnit 4
import org.junit.Test;
import static org.junit.Assert.assertEquals;import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;Jupiter rulează doar testele adnotate cu @Test din pachetul lui.
2Gradle nu folosește platforma JUnit 5
test {
}tasks.named("test") {
useJUnitPlatform()
}Fără useJUnitPlatform(), Gradle caută teste JUnit 4.
3Metoda de test e private
@Test
private void total() { }@Test
void total() { }JUnit 5 nu rulează metode de test private.
Repară eroarea, direct în pagină
Testul folosește @Test și assertEquals din JUnit 4 (org.junit), iar în proiectul Spring Boot 3 build-ul raportează No tests found. Treci testul pe JUnit 5: folosește @org.junit.jupiter.api.Test și Assertions.assertEquals din org.junit.jupiter.api.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.stereotype.Service;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
record Produs(long id, String nume, int pret) { }
@Service
class CosService {
int total(List<Integer> preturi) {
int s = 0;
for (int p : preturi) {
s += p;
}
return s;
}
int reducere(int total, int procent) {
if (procent < 0 || procent > 100) {
throw new IllegalArgumentException("Procent invalid: " + procent);
}
return total - total * procent / 100;
}
}
@RestController
class ProdusController {
private final Map<Long, Produs> produse = new TreeMap<>(Map.of(
1L, new Produs(1, "Mouse", 89),
2L, new Produs(2, "Tastatură", 199)));
@GetMapping("/api/produse/{id}")
Produs unul(@PathVariable long id) {
Produs p = produse.get(id);
if (p == null) {
throw new org.springframework.web.server.ResponseStatusException(org.springframework.http.HttpStatus.NOT_FOUND);
}
return p;
}
@GetMapping("/api/salut")
String salut(@RequestParam String nume) {
return "Salut, " + nume + "!";
}
}
@SpringBootTest
class CosServiceTest {
@Autowired
CosService cos;
@org.junit.Test
public void totalulCosului() {
org.junit.Assert.assertEquals(288, cos.total(List.of(89, 199)));
}
}Tomcat started on port 8080 (http) Started Application CosServiceTest OK totalulCosului() Teste: 1 trecute, 0 picate
Indiciu
Înlocuiește org.junit.Test și org.junit.Assert cu echivalentele din org.junit.jupiter.api.
Reparația
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.stereotype.Service;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
record Produs(long id, String nume, int pret) { }
@Service
class CosService {
int total(List<Integer> preturi) {
int s = 0;
for (int p : preturi) {
s += p;
}
return s;
}
int reducere(int total, int procent) {
if (procent < 0 || procent > 100) {
throw new IllegalArgumentException("Procent invalid: " + procent);
}
return total - total * procent / 100;
}
}
@RestController
class ProdusController {
private final Map<Long, Produs> produse = new TreeMap<>(Map.of(
1L, new Produs(1, "Mouse", 89),
2L, new Produs(2, "Tastatură", 199)));
@GetMapping("/api/produse/{id}")
Produs unul(@PathVariable long id) {
Produs p = produse.get(id);
if (p == null) {
throw new org.springframework.web.server.ResponseStatusException(org.springframework.http.HttpStatus.NOT_FOUND);
}
return p;
}
@GetMapping("/api/salut")
String salut(@RequestParam String nume) {
return "Salut, " + nume + "!";
}
}
@SpringBootTest
class CosServiceTest {
@Autowired
CosService cos;
@Test
void totalulCosului() {
assertEquals(288, cos.total(List.of(89, 199)));
}
}De ce merge
Motorul JUnit 5 rulează doar metodele cu @Test din org.junit.jupiter.api; adnotarea org.junit.Test e a JUnit 4 și e ignorată, deci testul nu rula deloc. Importurile org.junit.jupiter.api.* și Assertions.* erau deja în fișier, așa că ajunge @Test simplu și assertEquals. Bonus: metodele de test nu mai trebuie să fie public.
Alte forme ale mesajului
No tests were foundTests run: 0, Failures: 0, Errors: 0, Skipped: 0No tests found in CosServiceTest
Cum o eviți pe viitor
- Verifică importurile în fiecare test nou: totul din
org.junit.jupiter.api. - Exclude JUnit 4 din dependențe dacă nu-l folosești, ca IDE-ul să nu-l mai propună.
- Urmărește numărul de teste rulate în build: o scădere bruscă e un semnal.
- Numește clasele de test cu sufixul
Test.
Întrebări frecvente
Pot rula și teste JUnit 4 în Spring Boot 3?
Da, cu dependența junit-vintage-engine. E utilă doar la migrare; testele noi se scriu în JUnit 5.
Laboratorul rulează testul și cu importul vechi. De ce?
Simulatorul tratează ambele adnotări la fel. În proiect, platforma JUnit 5 fără motorul vintage ignoră testul.
Erori înrudite
org.junit.platform.commons.JUnitException: @BeforeAll method 'void CosServiceTest.pregateste()' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS). JUnit 5 rulează metodele @BeforeAll o singură dată, înainte să creeze vreo instanță a clasei de test, deci ele trebuie să fie static. Fă metoda static (și câmpurile pe care le setează), sau pune @TestInstance(Lifecycle.PER_CLASS) pe clasă. org.opentest4j.AssertionFailedError: expected: <288> but was: <199> O aserțiune assertEquals din JUnit 5 a comparat valoarea așteptată (primul argument) cu cea reală (al doilea) și au ieșit diferite. Fie codul testat are un bug, fie așteptarea din test e greșită. Recalculează de mână rezultatul corect și verifică ordinea argumentelor. Învață Spring Boot de la zero, cu exerciții la fiecare lecție
Curs Spring Boot și REST API: lecții scurte, fiecare cu un exercițiu verificat automat, direct în browser. Primele 18 lecții sunt gratuite.
