2017-02-08 2 views
2

Я использую Spring-boot-starter-parent 1.4.3.RELEASE и написал тестовые примеры, используя mockito-all 2.0.2-beta. После использования @MockBean Моя весна началась в два раза ..Почему приложение загрузки Spring запускалось дважды

2017-02-08 12:03:11.135 INFO 9375 --- [   main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$61cd4862] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 

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

2017-02-08 12:03:11.268 INFO 9375 --- [   main] c.v.d.chain.PublisherChainResourceTest : No active profile set, falling back to default profiles: default 
2017-02-08 12:03:11.271 INFO 9375 --- [   main] o.s.w.c.s.GenericWebApplicationContext : Refreshing org.s[email protected]101d4a4e: startup date [Wed Feb 08 12:03:11 IST 2017]; parent: org.spring[email protected]4adfb1f 

........................

2017-02-08 12:03:24.109 INFO 9375 --- [   main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$61cd4862] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 

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

2017-02-08 12:03:24.267 INFO 9375 --- [   main] c.v.d.t.AdNetworkParamResourceTest  : No active profile set, falling back to default profiles: default 
2017-02-08 12:03:24.272 INFO 9375 --- [   main] o.s.w.c.s.GenericWebApplicationContext : Refreshing org.s[email protected]7fcc9949: startup date [Wed Feb 08 12:03:24 IST 2017]; parent: org.spring[email protected]17660e9a 

Мое приложение .yml,

spring: 
    datasource: 
    url: jdbc:hsqldb:mem:testdb;sql.syntax_mys=true 
    username: 
    password: 
    driver-class-name: org.hsqldb.jdbcDriver 
    testOnBorrow: true 
    validationQuery: SELECT 1 
    jpa: 
    hibernate: 
     ddl-auto: create-drop 
    properties: 
     hibernate: 
     format_sql: true 
     show_sql: true 
endpoints: 
    health: 
    sensitive: false 
server: 
    tomcat: 
    basedir: target/tomcat 
    accesslog: 
     enabled: true 
     pattern: "%v %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i" %S %D" 
liquibase: 
    user: 
    password: 
    default-schema: your_schema 
    enabled: false 

Как это решить? Почему весна начинается в два раза, когда я бегу «МВН чистой установки»

Мой тест следующим образом ..

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = ConfigMyApplication.class) 
@WebAppConfiguration 
public class XXResourceTest extends AbstractTransactionalJUnit4SpringContextTests { 

    private static final String PATH = "/xxxxxxx/"; 

    @Autowired 
    XXXXXRepository xxxxxRepository; 

    @MockBean 
    @Autowired 
    XXXXXApiService xxxxxApiService; 

    @Autowired 
    WebApplicationContext webApplicationContext; 

    MockMvc mockMvc; 
    HttpHeaders headers; 

    int xxxId = 1; 
    int parentId = 1; 

    @Before 
    public void setUp() { 
     this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 
     MockitoAnnotations.initMocks(this); 

     headers = new HttpHeaders(); 
     headers.add(CONTENT_TYPE, APPLICATION_JSON); 
    } 

    @Test 
    public void createXXXTest() throws JsonProcessingException, Exception { 
     DtoInput input = new DtoInput(); 
     input.setXXXId(xxxId); 
     input.setParentId(parentId); 

     Parent parent = new Parent(); 
     parent.setId(parentId); 

     when(xxxxxApiService.getParent(parentId)).thenReturn(parent); 

     mockMvc.perform(post(PATH).content(new ObjectMapper().writeValueAsString(input)).headers(headers)) 
       .andDo(print()).andExpect(status().isCreated()) 
       .andExpect(jsonPath("$.xxId", is(xxId))) 
       .andExpect(jsonPath("$.type", is("type"))) 
       .andExpect(jsonPath("$.parentId", is(parentId))); 
    } 
+0

Показать тестовый класс. – notionquest

+0

@SST Содержит ли ваш ConfigMyApplication.class как SpringBootApplication? Вместо этого попробуйте использовать аннотацию TestConfiguration – rvit34

ответ

-1

У вас есть несколько тестов с различными пружинными контекстов, поэтому новый контекст запускается для каждого.

Весна будет кэшировать контексты между тестами, если они идентичны, но если у вас есть разные beans/config, тогда ему нужно создать кратные.