title: Spring MongoRepository, where to catch the exception?
body: <p>I have a Spring project, and a MongoRepository. The MongoRepository is an Interface that extends MongoRepository, just like JPA.</p>
  
  <p>If I try to build my project with <code>mvn clean install</code> it runs Spring once. Spring tries to connect to MongoDB which is not running on my Jenkins server. </p>
  
  <blockquote>
    <p>exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}</p>
  </blockquote>
  
  <p>Is there a way to catch the exception? I cannot catch it on the service where I call my repository, because those methods are not executed. I think it has something to do with <code>@autowire</code>, but I can't figure out how to catch the exception.</p>
  
  <p>The architecture:</p>
  
  <pre><code>application
    - resource (api)
    - service
    - repository extends MongoRepository
  </code></pre>
  
  <p>The application scans the project, the resource calls the service, the service calls the repository and the repository throws an error because it cannot connect to MongoDB.</p>
  
  <p><strong>Repository:</strong></p>
  
  <pre><code>public interface MetingRepository extends MongoRepository&lt;Meting, String&gt; {
      Page&lt;Meting&gt; findAllByRuimteId(String ruimteId, Pageable page);
  }
  </code></pre>
  
  <p><strong>Service:</strong></p>
  
  <pre><code>@Service("metingenService")
  public class MetingServiceImpl implements MetingService {
  
    // could I try-catch this?
    @Autowired
    private MetingRepository metingRepository;
  
      @Override
      public Meting addMeting(Meting meting) {
        // try-catch does not solve the issue here
        return metingRepository.save(meting);
      }
    }
  }
  </code></pre>
  
  <p>The only <strong>test</strong> I have, automatically generated:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest
  public class MetingenServiceApplicationTests {
  
      @Test
      public void contextLoads() {
  
      }
  
  }
  </code></pre>
  
  <p><strong>Stacktrace:</strong></p>
  
  <blockquote>
    <p>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'metingResource': Unsatisfied dependency expressed through field 'metingService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'metingenService': Unsatisfied dependency expressed through field 'metingRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'metingRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]</p>
  </blockquote>