BAEL-88 Testing in Spring Boot (#1653)
* yasin.bhojawala@gmail.com Evaluation article on Different Types of Bean Injection in Spring * Revert "yasin.bhojawala@gmail.com" This reverts commit 963cc51a7a15b75b550108fe4e198cd65a274032. * Fixing compilation error and removing unused import * Introduction to Java9 StackWalking API - yasin.bhojawala@gmail.com Code examples for the article "Introduction to Java9 StackWalking API" * BAEL-608 Introduction to Java9 StackWalking API * BAEL-608 Introduction to Java9 StackWalking API changing the test names to BDD style * BAEL-608 Introduction to Java9 StackWalking API correcting the typo * BAEL-608 Introduction to Java9 StackWalking API improving method names * BAEL-608 Introduction to Java9 StackWalking API test method names improvements * BAEL-718 Quick intro to javatuples * merging pom from master * BAEL-722 Intro to JSONassert * BAEL-722 Intro to JSONassert Updated to 1.5.0 * BAEL-745 Quick Math.pow example * BAEL-729 Adding custom info to actuator's /info endpoint * BAEL-88 Testing in Spring Boot * BAEL-88 Testing in Spring Boot
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package org.baeldung.boot.boottest;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
@Entity
|
||||
@Table(name = "person")
|
||||
public class Employee {
|
||||
|
||||
public Employee() {
|
||||
}
|
||||
|
||||
public Employee(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Size(min = 3, max = 20)
|
||||
private String name;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.baeldung.boot.boottest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Transactional
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
|
||||
public Optional<Employee> findByName(String name);
|
||||
|
||||
public Optional<Employee> findById(Long id);
|
||||
|
||||
public List<Employee> findAll();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.baeldung.boot.boottest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class EmployeeRestController {
|
||||
|
||||
@Autowired
|
||||
EmployeeService employeeService;
|
||||
|
||||
@RequestMapping(value = "/employees", method = RequestMethod.POST)
|
||||
public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
|
||||
HttpStatus status = HttpStatus.CREATED;
|
||||
Employee saved = employeeService.save(employee);
|
||||
return new ResponseEntity<>(saved, status);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/employees", method = RequestMethod.GET)
|
||||
public List<Employee> getAllEmployees() {
|
||||
return employeeService.getAllEmployees();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.baeldung.boot.boottest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeService {
|
||||
|
||||
public Optional<Employee> getEmployeeById(Long id);
|
||||
|
||||
public Optional<Employee> getEmployeeByName(String name);
|
||||
|
||||
public List<Employee> getAllEmployees();
|
||||
|
||||
public boolean exists(String email);
|
||||
|
||||
public Employee save(Employee employee);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.baeldung.boot.boottest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
@Autowired
|
||||
EmployeeRepository employeeRepository;
|
||||
|
||||
@Override
|
||||
public Optional<Employee> getEmployeeById(Long id) {
|
||||
return employeeRepository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Employee> getEmployeeByName(String name) {
|
||||
return employeeRepository.findByName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(String name) {
|
||||
if (employeeRepository.findByName(name) != null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee save(Employee employee) {
|
||||
return employeeRepository.save(employee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Employee> getAllEmployees() {
|
||||
return employeeRepository.findAll();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user