spring-boot/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/sample/data/jpa/SampleDataJpaApplicationTes...

81 lines
2.6 KiB
Java
Raw Normal View History

2014-11-19 05:29:54 +08:00
/*
* Copyright 2012-2016 the original author or authors.
2014-11-19 05:29:54 +08:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2015-09-06 12:36:18 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
2014-11-19 05:29:54 +08:00
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sample.data.jpa;
2013-04-24 17:02:07 +08:00
import java.lang.management.ManagementFactory;
import javax.management.ObjectName;
import org.junit.Before;
2013-04-24 17:02:07 +08:00
import org.junit.Test;
2013-10-03 03:07:04 +08:00
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
2013-10-03 03:07:04 +08:00
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
2013-04-24 17:02:07 +08:00
import static org.assertj.core.api.Assertions.assertThat;
2014-11-04 09:20:40 +08:00
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2013-04-24 17:02:07 +08:00
/**
* Integration test to run the application.
2014-07-03 05:43:43 +08:00
*
* @author Oliver Gierke
* @author Dave Syer
2013-04-24 17:02:07 +08:00
*/
@RunWith(SpringRunner.class)
@SpringBootTest
// Enable JMX so we can test the MBeans (you can't do this in a properties file)
@TestPropertySource(properties = { "spring.jmx.enabled:true",
"spring.datasource.jmx-enabled:true" })
2013-11-16 15:42:40 +08:00
@ActiveProfiles("scratch")
// Separate profile for web tests to avoid clashing databases
2013-10-03 03:07:04 +08:00
public class SampleDataJpaApplicationTests {
2013-04-24 17:02:07 +08:00
@Autowired
private WebApplicationContext context;
2013-04-24 17:02:07 +08:00
private MockMvc mvc;
2013-04-24 17:02:07 +08:00
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
2013-04-24 17:02:07 +08:00
}
@Test
public void testHome() throws Exception {
this.mvc.perform(get("/")).andExpect(status().isOk())
.andExpect(content().string("Bath"));
2013-04-24 17:02:07 +08:00
}
@Test
public void testJmx() throws Exception {
assertThat(ManagementFactory.getPlatformMBeanServer()
.queryMBeans(new ObjectName("jpa.sample:type=ConnectionPool,*"), null))
.hasSize(1);
}
2013-04-24 17:02:07 +08:00
}