Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 测试@RestController时注入模拟服务_Spring_Unit Testing_Spring Boot - Fatal编程技术网

Spring 测试@RestController时注入模拟服务

Spring 测试@RestController时注入模拟服务,spring,unit-testing,spring-boot,Spring,Unit Testing,Spring Boot,我正在测试一个用@RestController注释的REST控制器,我想注入模拟服务 我在这里寻求帮助,但运气不好 我正在使用以下注释启动spring启动应用程序的测试: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebIntegrationTest 其中,Application.class用@SpringBootApplicatio

我正在测试一个用
@RestController
注释的REST控制器,我想注入模拟服务

我在这里寻求帮助,但运气不好

我正在使用以下注释启动spring启动应用程序的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest
其中,
Application.class
@SpringBootApplication
注释,但我似乎找不到在我的控制器上注入模拟依赖项的方法,例如:

@Mock
private BlogPostService blogPostService;

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
}
这是我看到的堆栈跟踪,它在超时后失败,因为它试图将所有依赖项注入数据库(我正试图避免在服务级别引入模拟):

com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase
13:30:12.924[main]调试o.s.t.c.j.SpringJUnit4ClassRunner-使用[class com.xxx.cms.web.resources.BlogPostController WithTemplateTestCase]调用的SpringJUnit4ClassRunner构造函数
13:30:12.934[main]DEBUG o.s.test.context.BootstrapUtils-从类[org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]实例化CacheAwareContextLoaderDelegate]
13:30:12.943[main]DEBUG o.s.test.context.BootstrapUtils-使用构造函数[public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate]实例化BootstrapContext]
13:30:12.965[main]调试o.s.test.context.Bootstrapputils-从类[org.springframework.boot.test.WebAppIntegrationTestContextBootstrapper]为测试类[com.xxx.cms.web.resources.BlogPostController WithTemplateTestCase]实例化TestContextBootstrapper
13:30:12.998[main]调试o.s.b.t.WebAppIntegrationTestContextBootstrapper-找到上下文配置属性的显式ContextLoader类[org.springframework.boot.test.SpringApplicationContextLoader][ContextConfigurationAttributes@2b552920declaringClass='com.xxx.cms.web.resources.BlogPostController WithTemplateTestCase',classes='{class com.xxx.cms.web.Application}',locations='{}',inheritLocations=true,initializers='{}',inheritInitializers=true,name=[null],contextLoaderClass='org.springframework.boot.test.SpringApplicationContextLoader']
13:30:13.015[main]调试o.s.t.c.s.AbstractContextLoader-未检测到测试类[com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]:类路径资源[com/xxx/cms/web/resources/BlogPostControllerWithTemplateTestCase context.xml]不存在
13:30:13.016[main]调试o.s.t.c.s.AbstractContextLoader-未检测到测试类[com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]:类路径资源[com/xxx/cms/web/resources/BlogPostControllerWithTemplateTestCaseContext.groovy]不存在
13:30:13.016[main]INFO o.s.t.c.s.AbstractContextLoader-无法检测测试类[com.xxx.cms.web.resources.BlogPostController WithTemplateTestCase]的默认资源位置:未找到后缀{-context.xml,context.groovy}的资源。
13:30:13.019[main]调试o.s.t.c.support.ActiveProfilesUtils-找不到注释类型[org.springframework.test.context.ActiveProfiles]和类[com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]的“注释声明类”
13:30:13.026[main]调试o.s.test.util.ReflectionTestUtils-在目标对象上设置[null]类型的字段“propertySourceProperties”[[WebMergedContextConfiguration@5f282abbtestClass=BlogPostControllerWithTemplateTestCase,locations='{}',classes='{class com.xxx.cms.web.Application}',contextInitializerClasses='[]',activeProfiles='{},propertySourceLocations='{}',propertySourceProperties='{}',resourceBasePath='',contextLoader='org.springframework.boot.test.SpringApplicationContextLoader',parent=[null]]或目标类[class org.springframework.test.context.web.WebMergedContextConfiguration]转换为值[[Ljava.lang.String;@167fdd33]
13:30:13.027[main]调试o.s.b.t.WebAppIntegrationTestContextBootstrapper-@TestExecutionListeners不存在于类[com.xxx.cms.web.resources.BlogPostController WithTemplateTestCase]:使用默认值。
13:30:13.036[main]INFO o.s.b.t.WebAppIntegrationTestContextBootstrapper-从位置[META-INF/spring.factories]加载默认TestExecutionListener类名:[org.springframework.test.context.web.ServletTestExecutionListener,org.springframework.test.context.support.dirtiesContextBeforeMistestExecutionListener,org.springframework.test.context.support.DependencyInjectionTestExecutionListener,org.springframework.test.context.support.DirtiesContextTestExecutionListener,org.springframework.test.contextt、 transaction.TransactionalTestExecutionListener,org.springframework.test.context.jdbc.sqlscriptsteExecutionListener]
13:30:13.060[main]信息o.s.b.t.WebAppIntegrationTestContextBootstrapper-使用TestExecutionListeners:[org.springframework.test.context.web。ServletTestExecutionListener@7b69c6ba,org.springframework.test.context.support。DirtiesContextBeforeModesTestExecutionListener@46daef40,org.springframework.test.context.support。DependencyInjectionTestExecutionListener@12f41634,org.springframework.test.context.support。DirtiesContextTestExecutionListener@13c27452,org.springframework.test.context.transaction。TransactionalTestExecutionListener@262b2c86,org.springframework.test.context.jdbc。SqlScriptsTestExecutionListener@371a67ec]
13:30:13.063[main]调试o.s.t.annotation.ProfileValueUtils-为测试类[com.xxx.cms.web.resources.BlogPostController WithTemplateTestCase]检索@ProfileValueSourceConfiguration[null]
13:30:13.064[main]调试o.s.t.annotation.ProfileValueUtils-为类[com.xxx.cms.web.resources.BlogPostControllerWithTemplates]检索ProfileValueSource类型[class org.springframework.test.annotation.SystemProfileValueSource]
    com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase
13:30:12.924 [main] DEBUG o.s.t.c.j.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:12.934 [main] DEBUG o.s.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
13:30:12.943 [main] DEBUG o.s.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
13:30:12.965 [main] DEBUG o.s.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase] from class [org.springframework.boot.test.WebAppIntegrationTestContextBootstrapper]
13:30:12.998 [main] DEBUG o.s.b.t.WebAppIntegrationTestContextBootstrapper - Found explicit ContextLoader class [org.springframework.boot.test.SpringApplicationContextLoader] for context configuration attributes [ContextConfigurationAttributes@2b552920 declaringClass = 'com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase', classes = '{class com.xxx.cms.web.Application}', locations = '{}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.boot.test.SpringApplicationContextLoader']
13:30:13.015 [main] DEBUG o.s.t.c.s.AbstractContextLoader - Did not detect default resource location for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]: class path resource [com/xxx/cms/web/resources/BlogPostControllerWithTemplateTestCase-context.xml] does not exist
13:30:13.016 [main] DEBUG o.s.t.c.s.AbstractContextLoader - Did not detect default resource location for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]: class path resource [com/xxx/cms/web/resources/BlogPostControllerWithTemplateTestCaseContext.groovy] does not exist
13:30:13.016 [main] INFO  o.s.t.c.s.AbstractContextLoader - Could not detect default resource locations for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]: no resource found for suffixes {-context.xml, Context.groovy}.
13:30:13.019 [main] DEBUG o.s.t.c.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.026 [main] DEBUG o.s.test.util.ReflectionTestUtils - Setting field 'propertySourceProperties' of type [null] on target object [[WebMergedContextConfiguration@5f282abb testClass = BlogPostControllerWithTemplateTestCase, locations = '{}', classes = '{class com.xxx.cms.web.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', resourceBasePath = '', contextLoader = 'org.springframework.boot.test.SpringApplicationContextLoader', parent = [null]]] or target class [class org.springframework.test.context.web.WebMergedContextConfiguration] to value [[Ljava.lang.String;@167fdd33]
13:30:13.027 [main] DEBUG o.s.b.t.WebAppIntegrationTestContextBootstrapper - @TestExecutionListeners is not present for class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]: using defaults.
13:30:13.036 [main] INFO  o.s.b.t.WebAppIntegrationTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
13:30:13.060 [main] INFO  o.s.b.t.WebAppIntegrationTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7b69c6ba, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@46daef40, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@12f41634, org.springframework.test.context.support.DirtiesContextTestExecutionListener@13c27452, org.springframework.test.context.transaction.TransactionalTestExecutionListener@262b2c86, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@371a67ec]
13:30:13.063 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.064 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]13:30:13.105 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.105 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]13:30:13.107 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.107 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.108 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.108 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.115 [main] DEBUG o.s.t.c.s.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@74a10858 testClass = BlogPostControllerWithTemplateTestCase, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5f282abb testClass = BlogPostControllerWithTemplateTestCase, locations = '{}', classes = '{class com.xxx.cms.web.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.IntegrationTest=true}', resourceBasePath = '', contextLoader = 'org.springframework.boot.test.SpringApplicationContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null].
13:30:13.124 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.125 [main] DEBUG o.s.t.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase]
13:30:13.132 [main] DEBUG o.s.t.c.s.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@74a10858 testClass = BlogPostControllerWithTemplateTestCase, testInstance = com.xxx.cms.web.resources.BlogPostControllerWithTemplateTestCase@75881071, testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5f282abb testClass = BlogPostControllerWithTemplateTestCase, locations = '{}', classes = '{class com.xxx.cms.web.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.IntegrationTest=true}', resourceBasePath = '', contextLoader = 'org.springframework.boot.test.SpringApplicationContextLoader', parent = [null]]]].
13:30:13.259 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
13:30:13.261 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
13:30:13.261 [main] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
13:30:13.263 [main] DEBUG o.s.core.env.StandardEnvironment - Adding [integrationTest] PropertySource with search precedence immediately lower than [systemEnvironment]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.1.RELEASE)

2016-01-18 13:30:14.252  INFO 3685 --- [           main] r.BlogPostControllerWithTemplateTestCase : Starting BlogPostControllerWithTemplateTestCase on Matteos-MacBook-Pro.local with PID 3685 (/Users/mox/Dati/Progetti/xxx/cms/cms-parent-pom/cms-web/target/test-classes started by mox in /Users/mox/Dati/Progetti/xxx/cms/cms-parent-pom/cms-web)
2016-01-18 13:30:14.253  INFO 3685 --- [           main] r.BlogPostControllerWithTemplateTestCase : No active profile set, falling back to default profiles: default
2016-01-18 13:30:14.362  INFO 3685 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@36902638: startup date [Mon Jan 18 13:30:14 CET 2016]; root of context hierarchy
2016-01-18 13:30:17.504  INFO 3685 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-18 13:30:17.521  INFO 3685 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-18 13:30:17.525  INFO 3685 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-18 13:30:17.693  INFO 3685 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-18 13:30:17.694  INFO 3685 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3337 ms
2016-01-18 13:30:17.887  INFO 3685 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-18 13:30:17.892  INFO 3685 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-18 13:30:18.528  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post/{id}],methods=[PUT]}" onto public org.springframework.http.ResponseEntity<com.xxx.cms.services.dtos.BlogPostDTO> com.xxx.cms.web.resources.BlogPostController.update(com.xxx.cms.services.dtos.BlogPostDTO)
2016-01-18 13:30:18.528  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post/{id}],methods=[DELETE]}" onto public org.springframework.http.ResponseEntity<com.xxx.cms.services.dtos.BlogPostDTO> com.xxx.cms.web.resources.BlogPostController.delete(long)
2016-01-18 13:30:18.529  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[POST]}" onto public org.springframework.http.ResponseEntity<com.xxx.cms.services.dtos.BlogPostDTO> com.xxx.cms.web.resources.BlogPostController.create(com.xxx.cms.services.dtos.BlogPostDTO)
2016-01-18 13:30:18.529  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<com.xxx.cms.services.dtos.BlogPostDTO> com.xxx.cms.web.resources.BlogPostController.findById(long)
2016-01-18 13:30:18.529  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/post],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.xxx.cms.services.dtos.BlogPostDTO>> com.xxx.cms.web.resources.BlogPostController.findAll()
2016-01-18 13:30:18.529  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/{player}]}" onto public com.xxx.cms.persistence.domain.model.Message com.xxx.cms.web.resources.HelloWorldRestController.message(java.lang.String)
2016-01-18 13:30:18.537  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-01-18 13:30:18.538  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-01-18 13:30:18.580  INFO 3685 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/resources/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-18 13:30:18.655  INFO 3685 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@36902638: startup date [Mon Jan 18 13:30:14 CET 2016]; root of context hierarchy
2016-01-18 13:30:18.786  INFO 3685 --- [           main] c.i.c.p.config.MongoConfiguration        : localhost:27017/smart-cms
2016-01-18 13:30:19.264  INFO 3685 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-18 13:30:19.270  INFO 3685 --- [           main] r.BlogPostControllerWithTemplateTestCase : Started BlogPostControllerWithTemplateTestCase in 5.996 seconds (JVM running for 7.084)2016-01-18 13:30:19.756  INFO 3685 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-18 13:30:19.756  INFO 3685 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-18 13:30:19.771  INFO 3685 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 15 ms
2016-01-18 13:30:29.844 ERROR 3685 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]] with root cause

com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting to connect. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]
    at com.mongodb.BaseCluster.getDescription(BaseCluster.java:128) ~[mongo-java-driver-2.13.3.jar:na]
    at com.mongodb.DBTCPConnector.getClusterDescription(DBTCPConnector.java:402) ~[mongo-java-driver-2.13.3.jar:na]
    at com.mongodb.DBTCPConnector.getType(DBTCPConnector.java:579) ~[mongo-java-driver-2.13.3.jar:na]
    at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:376) ~[mongo-java-driver-2.13.3.jar:na]
    at com.mongodb.Mongo.isMongosConnection(Mongo.java:622) ~[mongo-java-driver-2.13.3.jar:na]
    at com.mongodb.DBCollection.findOne(DBCollection.java:936) ~[mongo-java-driver-2.13.3.jar:na]
    at com.mongodb.DBCollection.findOne(DBCollection.java:914) ~[mongo-java-driver-2.13.3.jar:na]
    at com.mongodb.DBCollection.findOne(DBCollection.java:858) ~[mongo-java-driver-2.13.3.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2098) ~[spring-data-mongodb-1.8.2.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2082) ~[spring-data-mongodb-1.8.2.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1855) ~[spring-data-mongodb-1.8.2.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1672) ~[spring-data-mongodb-1.8.2.RELEASE.jar:na]
    at org.springframework.data.mongodb.core.MongoTemplate.findById(MongoTemplate.java:614) ~[spring-data-mongodb-1.8.2.RELEASE.jar:na]
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findOne(SimpleMongoRepository.java:119) ~[spring-data-mongodb-1.8.2.RELEASE.jar:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:483) ~[spring-data-commons-1.11.2.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:468) ~[spring-data-commons-1.11.2.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440) ~[spring-data-commons-1.11.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61) ~[spring-data-commons-1.11.2.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at com.sun.proxy.$Proxy66.findOne(Unknown Source) ~[na:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) ~[spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at com.sun.proxy.$Proxy66.findOne(Unknown Source) ~[na:na]
    at com.xxx.cms.services.BlogPostService.findTodoById(BlogPostService.java:54) ~[classes/:na]
    at com.xxx.cms.services.BlogPostService.findById(BlogPostService.java:43) ~[classes/:na]
    at com.xxx.cms.web.resources.BlogPostController.findById(BlogPostController.java:55) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_25]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_25]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_25]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.30.jar:8.0.30]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]

2016-01-18 13:30:29.905  INFO 3685 --- [       Thread-4] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@36902638: startup date [Mon Jan 18 13:30:14 CET 2016]; root of context hierarchy

Process finished with exit code 0
@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest {

}
@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest {
    @InjectMocks
    private MyController controller;
    @Mock
    private BlogPostService service;
}