@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class PortalApplication {
public static void main(String[] args) {
SpringApplication.run(PortalApplication.class, args);
// 主配置类
@SpringBootApplication
public class PortalApplication {
public static void main(String[] args) {
SpringApplication.run(PortalApplication.class, args);
// 测试配置类
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
在测试类中使用 @SpringBootTest 注解来指定测试配置类:
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.runner.RunWith;
import org.junit.Test;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class PortalApplicationTest {
@Test
public void contextLoads() {
// 测试内容
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
@TestConfiguration
public class TestConfig {
@Bean
public SomeBean someBean() {
return new SomeBean();
在测试类中使用 @Import 注解来导入测试配置类:
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.context.annotation.Import;
import org.junit.runner.RunWith;
import org.junit.Test;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PortalApplication.class)
@Import(TestConfig.class)
public class PortalApplicationTest {
@Test
public void contextLoads() {
// 测试内容