为什么Spring启动应用程序启动两次

时间:2017-02-08 08:47:53

标签: java spring-mvc spring-boot mockito junit4

我正在使用spring-boot-starter-parent 1.4.3.RELEASE并使用mockito-all 2.0.2-beta编写测试用例。使用@MockBean后我的弹簧开始了两次..

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.springframework.web.context.support.GenericWebApplicationContext@101d4a4e: startup date [Wed Feb 08 12:03:11 IST 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@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.springframework.web.context.support.GenericWebApplicationContext@7fcc9949: startup date [Wed Feb 08 12:03:24 IST 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@17660e9a

我的application.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

如何解决这个问题? 当我运行" mvn clean install"

时,为什么弹簧开始两次

我的测试如下..

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

1 个答案:

答案 0 :(得分:0)

您有多个具有不同spring-contexts的测试,因此为每个测试启动了一个新的上下文。

如果测试相同,Spring会在测试之间缓存上下文,但是如果你有不同的bean / config,那么它需要创建倍数。

相关问题