Are these JUnit test methods correctly written in my Spring Boot application?

  Kiến thức lập trình

I am developing a Spring Boot application and have written some JUnit test methods to test my StudentCrud class. I would like to know if these methods are correctly written and follow best practices. Here is the code:

@SpringBootTest
public class StudentCrud {

    StudentServiceImplFactory studentService = new StudentServiceImplFactory();

    @Test
    public void test01Insert() {
        studentService.insert(new StudentVO("John"));
        studentService.insert(new StudentVO("Mike"));
        studentService.insert(new StudentVO("Alex"));
        assertEquals(1, studentService.insert(new StudentVO("John")));
    }

    @Test
    public void test02Delete() {
        assertEquals(1, studentService.delete(studentService.findByName("Mike").get()));
    }

    @Test
    public void test03Update() {
        StudentVO student = studentService.findByName("Alex").get();
        student.setName("Alexander");
        assertEquals(1, studentService.update(student));
    }

    @Test
    public void test03FindAll() {
        assertTrue(studentService.findAll().isPresent());
    }
}

Are these test methods written correctly? Are there any improvements or best practices I should follow for writing JUnit tests in a Spring Boot application?

New contributor

mike is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

LEAVE A COMMENT