Answer a question

I have a Spring project, and a MongoRepository. The MongoRepository is an Interface that extends MongoRepository, just like JPA.

If I try to build my project with mvn clean install it runs Spring once. Spring tries to connect to MongoDB which is not running on my Jenkins server.

exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}

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 @autowire, but I can't figure out how to catch the exception.

The architecture:

application
  - resource (api)
  - service
  - repository extends MongoRepository

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.

Repository:

public interface MetingRepository extends MongoRepository<Meting, String> {
    Page<Meting> findAllByRuimteId(String ruimteId, Pageable page);
}

Service:

@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);
    }
  }
}

The only test I have, automatically generated:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MetingenServiceApplicationTests {

    @Test
    public void contextLoads() {

    }

}

Stacktrace:

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}}]

Answers

Your unit test is trying to load the complete Spring Context. Thus, it's trying to load a valid MongoTemplate to connect to a MongoDB instance.

In most cases, you shouldn't use @SpringBootTests (which is for integration tests), instead you can do a normal JUnit test:

@RunWith(JUnit4.class) // or @RunWith(MockitoJUnitRunner.class)
public class MetingenServiceApplicationTests {
    ...
}
Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐