I am facing a issue that even my Junit class doesn't give an error, Eclipse doesn't run my junit test(I am super new in Junit Testing but, i guess eclipse doesn't recognize my junit test methods.
here my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>otopark-yonetim</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>otopark-yonetim</name>
<description>otopark yonetimi icin web projesi</description>
<url />
<licenses>
<license />
</licenses>
<developers>
<developer />
</developers>
<scm>
<connection />
<developerConnection />
<tag />
<url />
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.giffing.bucket4j.spring.boot.starter</groupId>
<artifactId>bucket4j-spring-boot-starter</artifactId>
<version>0.7.0</version>
</dependency>
<!-- Jackson Dependency -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Jakarta Servlet Dependency -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot DevTools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and my project architecture looks like;
my AracService in an abstract class and here my class for junit tesing AracServiceTest;
package com.example.otopark_yonetim.services;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.example.otopark_yonetim.entities.Arac;
import com.example.otopark_yonetim.repositories.AracRepository;
import org.springframework.boot.test.context.SpringBootTest;
/**
*
*/
@SpringBootTest
class AracServiceTest {
@Mock
private AracRepository aracRepository;
@InjectMocks
private AracService aracService;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
aracService = new AracService(aracRepository) {
@Override
public double fiyatHesapla(long id) {
return 50.0;
}
};
}
@Test
void testAracGirisYapti() {
String plaka = "34XYZ12";
String aracTuru = "Otomobil";
Arac mockArac = Arac.builder().plaka(plaka).aracTuru(aracTuru).girisSaati(LocalDateTime.now()).icerde(true)
.build();
when(aracRepository.save(any(Arac.class))).thenReturn(mockArac);
Arac sonuc = aracService.aracGirisYapti(plaka, aracTuru);
assertNotNull(sonuc);
assertEquals(plaka, sonuc.getPlaka());
assertEquals(aracTuru, sonuc.getAracTuru());
assertTrue(sonuc.isIcerde());
verify(aracRepository, times(1)).save(any(Arac.class));
}
@Test
void testHesaplaDakikaFarki() {
LocalDateTime giris = LocalDateTime.now().minusMinutes(30);
LocalDateTime cikis = LocalDateTime.now();
double fark = aracService.hesaplaDakikaFarki(giris, cikis);
assertEquals(30.0, fark, 0.01);
}
@Test
void testAracCikisYapti() {
// Arrange
long aracId = 1L;
Arac mockArac = new Arac();
mockArac.setId(aracId);
mockArac.setIcerde(true);
when(aracRepository.findById(aracId)).thenReturn(java.util.Optional.of(mockArac));
when(aracRepository.save(mockArac)).thenReturn(mockArac);
// Act
Arac sonuc = aracService.aracCikisYapti(aracId);
// Assert
assertNotNull(sonuc);
assertFalse(sonuc.isIcerde());
verify(aracRepository, times(1)).findById(aracId);
verify(aracRepository, times(1)).save(mockArac);
}
}
is there any idea, where is my problem?
I tried to clean and update the project but it couldn't be helpful.
You have placed your test in src/test/resources
which is the location for non-java files. Your tests should be placed in src/test/java
(just as regular java goes in src/main/java
).