Reputation: 2144
I am using spring-boot-starter-parent 1.4.3.RELEASE and wrote test cases using mockito-all 2.0.2-beta. After using @MockBean My spring started twice..
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
My 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
How to solve this? Why the spring starts twice when I run "mvn clean install"
My test as follows..
@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)));
}
Upvotes: 2
Views: 3258
Reputation: 8300
You have multiple tests with different spring-contexts, so a new context is started for each.
Spring will cache contexts between tests if they are identical, but if you have different beans/config then it needs to create multiples.
Upvotes: 1