Search code examples
javamongodbspring-bootspring-boot-test

How to make RestTemplet available while writing integration test for repository while working with MongoDB


I am trying to write an integration test for my MongoRepository using @DataMongoTest with findById() and check if the document has some value or not, but the issue is with the RestTemplate I am using in my project. It is unable to bind a Bean to the restTemplate field even though I have given it the MainTest class. Below are the details.

I can run this application with local configuration and able to receive data from MongoDB. We are using this restTemplate bean in another Service class to call some backend service before fetching data from MongoDb.

My Main Class

@SpringBootApplication
public class User{
    public static void main(String[] args){
        SpringApplication.run(User.class,args);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder {
        return builder.build();}
    }
}

My Mongo Repository

Here my Document object is called Response and the Id is of type String

@Repository
public interface AccountRepository extends CurdRepository<Response, String> {

}

My Main Test Class

I have implemented the Bean as the main class, just in case thinking it might take reference from here. But it did not take reference from here

@SpringBootTest
class UserManagementTest {

    @Test
    void contextLoads() { }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) { 
        return builder.build();
    }

My Repository Test

@DataMongoTest
public class AccountRepositoryMongoIT {

    @Autowired
    AccountRepository repo;

    Response response = new Response();

    @BeforeEach
    void setUp(){
        response.setId("1234");
        response.setSomeValue("abcd")
    }
  
    @Test
    public void test_GetAccountByID() {
        assertEqual(response.getSomeValue(), 
        repo.findById(id).get().getSomeValue());
    }
}

But when I run this, it is failing with the following error:

UnsatisfiedDependencyException: Error creating bean with name 'restTemplate' defined in com.java.user.User.....

Is there a way that it can be run without using the @SpringBootTest annotation because we don't want to bring up the whole container? We just want to test this repo light weight.

Please let me know if I am missing anything here


Solution

  • When using the sliced @DataMongoTest annotation, Spring Test will scan for your main Spring Boot class (User in your example) and will use it for the sliced context initialization.

    As you define there the RestTemplate bean that requires the RestTemplateBuilder (injected via the method parameter), your context startup fails as this builder is not part of the sliced context that you get with @DataMongoTest. It is however available when starting the entire context, that's why it works with @SpringBooTest.

    Solution: Move out any bean definition from your main class. In your example you can outsource the RestTempalte creation to , e.g. a RestTemplateConfig class

    @Configuration
    class RestTemplateConfig {
      
      @Bean
      public RestTemplet restTemplet(RestTemplateBuilder builder){return builder.buil();}
    }