I have an experimental Spring Boot Service that uses MongoDB. I've tried to write some unit tests with the 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
dependency. However, the tests are generating an error:
java.lang.IllegalStateException: Failed to load ApplicationContext.
When running debug level logging the only additional info I see is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Could not start process: <EOF>
Here is the unit test class:
UserRepositoryTest
package com.gitgrub.userservice.repository;
import com.gitgrub.userservice.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.test.context.ActiveProfiles;
import static org.junit.jupiter.api.Assertions.*;
@DataMongoTest
@ActiveProfiles("test")
class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
void findByUsername() {
String testUsername = "testUser";
User user = new User(testUsername, "test@example.com", "password");
userRepository.save(user);
User foundUser = userRepository.findByUsername(testUsername);
assertNotNull(foundUser);
assertEquals(testUsername, foundUser.getUsername());
}
@Test
void findByEmail() {
String testEmail = "test@example.com";
User user = new User("testUser", testEmail, "password");
userRepository.save(user);
User foundUser = userRepository.findByEmail(testEmail);
assertNotNull(foundUser);
assertEquals(testEmail, foundUser.getEmail());
}
@Test
void findByUsernameOrEmail() {
String testUsername = "testUser";
String testEmail = "test@example.com";
User user1 = new User(testUsername, "user1@example.com", "password");
User user2 = new User("user2", testEmail, "password");
userRepository.save(user1);
userRepository.save(user2);
User foundUser1 = userRepository.findByUsernameOrEmail(testUsername, "");
assertNotNull(foundUser1);
assertEquals(testUsername, foundUser1.getUsername());
User foundUser2 = userRepository.findByUsernameOrEmail("", testEmail);
assertNotNull(foundUser2);
assertEquals(testEmail, foundUser2.getEmail());
}
}
Here is the main project interface that's being tested:
UserRepository
/**
* UserRepository is an interface for a data access object (DAO) that handles User data.
* It extends MongoRepository to leverage Spring Data MongoDB's functionality,
* including CRUD operations and database queries.
*
* This repository provides methods for finding a user by username, by email,
* or by either username or email.
*
* The repository is used by other components of the application (like services and controllers)
* to interact with the User data in the database.
*
*/
package com.gitgrub.userservice.repository;
import com.gitgrub.userservice.model.User;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface UserRepository extends MongoRepository<User, String> {
User findByUsername(String username);
User findByEmail(String email);
User findByUsernameOrEmail(String username, String email);
}