Skip to content

Instantly share code, notes, and snippets.

@mmafrar
Created June 28, 2021 17:53
Show Gist options
  • Save mmafrar/e258184cd3aff1290c261788ee630500 to your computer and use it in GitHub Desktop.
Save mmafrar/e258184cd3aff1290c261788ee630500 to your computer and use it in GitHub Desktop.
Writing unit tests in Spring Boot with JUnit 5
package com.example.demo.service;
import com.example.demo.model.Contact;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.persistence.EntityNotFoundException;
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ContactServiceTest {
@Autowired
ContactService contactService;
@BeforeAll
void setUp() {
Contact contact = new Contact();
contact.setName("Mohamed Afrar");
contact.setEmail("example@gmail.com");
contact.setCountry("Sri Lanka");
contactService.saveContact(contact);
}
@Test
void testFindAll() {
Assertions.assertFalse(contactService.findAll().isEmpty());
}
@Test
void testFindById() {
Contact contact = contactService.findById(1).orElseThrow(EntityNotFoundException::new);
Assertions.assertEquals("Mohamed Afrar", contact.getName());
}
@Test
void testUpdateContact() {
Contact contact = contactService.findById(1).orElseThrow(EntityNotFoundException::new);
contact.setEmail("example@outlook.com");
Contact updatedContact = contactService.updateContact(1, contact);
Assertions.assertEquals("example@outlook.com", updatedContact.getEmail());
}
@AfterAll
void tearDown() {
contactService.deleteById(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment