Skip to content

Instantly share code, notes, and snippets.

@code-machina
Last active October 31, 2020 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-machina/a0c2c2612cec4cfb96ca5fba3edcbe5d to your computer and use it in GitHub Desktop.
Save code-machina/a0c2c2612cec4cfb96ca5fba3edcbe5d to your computer and use it in GitHub Desktop.
Student Model Sample
package com.gbkim.student.dal;
import java.util.Optional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.gbkim.student.dal.entities.Student;
import com.gbkim.student.dal.repos.StudentRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StudentdalApplicationTests {
@Autowired
private StudentRepository studentRepository; // Dependency Injection
@Test
public void testCreateStudent() {
Student entity = new Student();
entity.setName("cert");
entity.setCourse("Java spring framework");
entity.setFee(30d);
studentRepository.save(entity);
}
@Test
public void testFindStudentById() {
// CrudRepository.findOne is replaced by .findById
Student student = studentRepository.findById(1l).orElse(null);
System.out.println(student);
}
@Test
public void testUpdateStudent() {
Student student = studentRepository.findById(2l).orElse(null);
student.setFee(40d);
studentRepository.save(student);
}
@Test
public void testDeleteStudent() {
// Create Empty Student Instance
Student student = new Student();
// Set id number, which point to a row that is what you want to delete .
student.setId(1l);
// Send a delete transaction.
studentRepository.delete(student);;
// Run it!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment