Testing User Repository Operations in spring-boot-examples
This test suite validates the UserRepository functionality in a Spring Boot application, focusing on user data persistence and retrieval operations. The tests cover core database operations including saving users, finding by criteria, and deletion.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
ityouknow/spring-boot-examples
2.x/spring-boot-web/src/test/java/com/neo/model/UserRepositoryTests.java
package com.neo.model;
import java.text.DateFormat;
import java.util.Date;
import com.neo.repository.UserRepository;
import org.junit.Assert;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTests {
@Autowired
private UserRepository userRepository;
@Test
public void test() throws Exception {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String formattedDate = dateFormat.format(date);
userRepository.save(new User("aa1", "[email protected]", "aa", "aa123456",formattedDate));
userRepository.save(new User("bb2", "[email protected]", "bb", "bb123456",formattedDate));
userRepository.save(new User("cc3", "[email protected]", "cc", "cc123456",formattedDate));
// Assert.assertEquals(9, userRepository.findAll().size());
Assert.assertEquals("bb2", userRepository.findByUserNameOrEmail("bb", "xxx126.com").getNickName());
userRepository.delete(userRepository.findByUserName("aa"));
}
}