0% found this document useful (0 votes)
12 views

Testing Student

Hhg

Uploaded by

justinjaveed901
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Testing Student

Hhg

Uploaded by

justinjaveed901
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

ENTITY

1.Student.java

package com.example.demorepository.entity;

import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Student implements Serializable{
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String name;

@Column(name = "enrollment_Id", nullable = false, unique = true)


private String enrollmentId;

public Student() {}

public Student(String name, String enrollmentId) {


this.name = name;
this.enrollmentId = enrollmentId;
}

public Long getId() {


return id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", enrollment=" +
enrollmentId + "]";
}
}

REPOSITORY
1.StudentRepository.interface

package com.example.demorepository.repository;
import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demorepository.entity.Student;

public interface StudentRepository extends JpaRepository<Student, Long>{


}

APPLICATION.PROPERTIES
spring.application.name=demorepository
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop

spring.h2.console.enabled=true
logging.level.route=off
logging.level.org.springframework=error
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.main.banner-mode=off
logging.pattern.console=%msg%n

MainApplicationTests

package com.example.demorepository;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demorepository.entity.Student;
import com.example.demorepository.repository.StudentRepository;

@SpringBootTest
public class MainApplicationTests {

@Autowired
private StudentRepository studentRepository;

@Test
public void testCrud() {
// Create
Student student = new Student("Alisa Simmons", "2022AN50123");
Student savedStudent = studentRepository.save(student);
assertNotNull(savedStudent.getId());

// Update
savedStudent.setName("Alissa Simmons");
Student updatedStudent = studentRepository.save(savedStudent);
assertEquals("Alissa Simmons", updatedStudent.getName());
// Read
Optional<Student> foundStudentOptional =
studentRepository.findById(savedStudent.getId());
assertTrue(foundStudentOptional.isPresent());
assertEquals(savedStudent.getId(), foundStudentOptional.get().getId());

// Delete
studentRepository.delete(foundStudentOptional.get());
assertFalse(studentRepository.existsById(savedStudent.getId()));
}
}

You might also like