This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Tests): creating integration tests, and improving unit tests per…
…formance
- Loading branch information
Showing
12 changed files
with
178 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/java/com/example/comerce/integration/repository/UserRepositoryIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.comerce.integration.repository; | ||
|
||
import com.example.comerce.core.entities.User; | ||
import com.example.comerce.core.repository.UserRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@DataJpaTest | ||
@Transactional | ||
public class UserRepositoryIntegrationTest { | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Test | ||
public void testSaveAndFindUser() { | ||
User user = new User(); | ||
user.setName("Alessandro Lima"); | ||
user.setEmail("alessandro@email.com"); | ||
user.setCpf("12345678901"); | ||
user.setTelephone("12345678901"); | ||
user.setPassword("senha123"); | ||
|
||
User savedUser = userRepository.save(user); | ||
assertNotNull(savedUser.getUser_id()); | ||
|
||
Optional<User> foundUser = userRepository.findById(savedUser.getUser_id()); | ||
assertTrue(foundUser.isPresent()); | ||
assertEquals(user.getName(), foundUser.get().getName()); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/com/example/comerce/integration/services/UserServiceIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.example.comerce.integration.services; | ||
|
||
import com.example.comerce.core.entities.User; | ||
import com.example.comerce.core.repository.UserRepository; | ||
import com.example.comerce.core.services.UserService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@Transactional // Garante que cada teste seja executado em uma transação que será revertida | ||
public class UserServiceIntegrationTest { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
private UUID userId; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
User user = new User(); | ||
user.setName("Alessandro Lima"); | ||
user.setEmail("alessandro@email.com"); | ||
user.setCpf("12345678901"); | ||
user.setTelephone("12345678901"); | ||
user.setPassword("senha123"); | ||
userId = userRepository.save(user).getUser_id(); | ||
} | ||
|
||
@Test | ||
public void testFindById() { | ||
Optional<User> foundUser = userService.findById(userId); | ||
|
||
assertTrue(foundUser.isPresent()); | ||
assertEquals(userId, foundUser.get().getUser_id()); | ||
} | ||
|
||
@Test | ||
public void testDeleteUser() { | ||
userService.delete(userId); | ||
Optional<User> foundUser = userService.findById(userId); | ||
|
||
assertFalse(foundUser.isPresent()); | ||
} | ||
} |