-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserRepositoryTest.java
More file actions
62 lines (49 loc) · 1.94 KB
/
UserRepositoryTest.java
File metadata and controls
62 lines (49 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.sda.ironhack.Personal.Finance.Tracker.webApp.repos;
import com.sda.ironhack.Personal.Finance.Tracker.webApp.entities.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBootTest
class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
private User user1;
@BeforeEach
void setup() {
user1 = new User("John","helloworld","John@gmail.com",211);
userRepository.save(user1);
}
@AfterEach
void teardown() {
userRepository.deleteById(25);
}
@Test
public void FindByUserName(){
User userFromDb = userRepository.findByUsername("Hector");
assertEquals("John", user1.getUsername());
}
@Test
public void FindByUserId() {
User userFromDb = userRepository.findByUserId(user1.getUserId());
// Check if the user is found
assertNotNull(userFromDb);
// Compare the found user with the original user (user1)
assertEquals(user1, userFromDb);
}
@Test
public void UpdateUserBalance() {
// Update the user's balance
user1.setBalance(300); // New balance value
// Save the updated User object to the database
userRepository.save(user1); // Save the updated User object
// Retrieve the user again to check if the balance was updated
User updatedUser = userRepository.findByUserId(user1.getUserId());
// Check if the balance was updated correctly
assertNotNull(updatedUser);
assertEquals(300, updatedUser.getBalance()); // Verify that the balance is now 311
}
}