I have recently started to learn spring boot junit testing using mockito, but i get stuck very often. Here's my problem:
I have created a simple addEmployeeTest, which results in null pointer exception due to my repository(I am storing the data in my postgres database), where i'm storing the data returns null somehow.
Here are my files:
Employee.java
package springbootproject.model;
import lombok.*;
import javax.persistence.*;
@Data
@Entity
@Builder
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email", nullable = false)
private String email;
}
EmployeeRepsitory.java
package springbootproject.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import springbootproject.model.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {}
EmployeeController.java
package springbootproject.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springbootproject.model.Employee;
import springbootproject.repository.EmployeeRepository;
@RestController
@RequestMapping("/api/v1")
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@PostMapping("/employees")
public Employee createEmployee(@RequestBody Employee employee) {
return this.employeeRepository.save(employee);
}
}
EmployeeControllerTest.java
package springbootproject.controller;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import springbootproject.model.Employee;
import springbootproject.repository.EmployeeRepository;
@DataJpaTest
public class EmployeeControllerTest {
@Autowired
private EmployeeRepository employeeRepository;
@Test
public void addEmployeeTest() {
Employee employee = Employee.builder()
.id(1L).firstName("abc").lastName("def").email("[email protected]")
.build();
employeeRepository.save(employee);
assertThat(employee.getId()).isGreaterThan(0L);
}
}
application.properties
spring.datasource.url = jdbc:postgresql://localhost:5432/employees
spring.datasource.username = postgres
spring.datasource.password = postgres
spring.jpa.show-sql = true
## Hiberate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
I have been stuck on this for a while, I dont know if its too basic or not. i dont quite understand. Thanks
This could be due to your repository being in a different package than your test class. Make sure that the package containing your repository is being scanned by Spring. You can do this by using
or similar annotations in your main application class.