Spring启动测试配置

时间:2016-07-19 22:45:44

标签: java spring-boot spring-test

我有一个带有主类的spring boot应用程序,如下所示:

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

现在我想测试我的服务并创建一个基础测试类:

@SpringApplicationConfiguration(Application.class)
public abstract class TestBase {
}

当我运行测试时,我得到例外:

Caused by: java.lang.IllegalArgumentException: Can not load an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
    at org.springframework.util.Assert.notNull(Assert.java:115)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:117)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)

然后我使用ContextConfiguration

更改我的基础测试类
@ContextConfiguration(classes = Application.class)
public abstract class TestBase {
}

这次我得到DataSource初始化错误。我想知道为什么它在第一种情况下失败,为什么在第二种情况下它没有加载我已配置数据源的 application.properties

谢谢!

4 个答案:

答案 0 :(得分:7)

类似的东西:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTest{

   @Autowire
   Foo foo //whatever you are testing

   @Test
   public void FooTest() throws Exception{
     Foo f = foo.getFooById("22");
     assertEquals("9B". f.getCode); 
   }
 //TODO look into MockMVC for testing services
}

答案 1 :(得分:3)

使用

进行测试的示例

Spring Boot

Spring Boot Test Configuration

JUnit5

FreeMarker

您将不会发现以下所有内容:)花了很长时间才能找出答案。

配置

@TestConfiguration
@PropertySource(value = "classpath:test.properties", encoding = "UTF-8")
public class GlobalConfig {

    @Bean(name = "JsonMapper")
    public JsonMapper jsonMapper() {

        return new JsonMapper();
    }

    @Bean(name = "ObjectMapper")
    public ObjectMapper objectMapper() {

        return new ObjectMapper();
    }

    @Bean(name = "Mapper")
    public Mapper dozer() {

        return new DozerBeanMapper();
    }

    @Bean(name = "Validator")
    public Validator validator() {

        return new DefaultValidatorAdapter();
    }

}

实际测试文件

import freemarker.template.Configuration;
import global.GlobalConfig;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;


@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {GlobalConfig.class, MessagePersistManager.class, TemplateManager.class, FileOperationsManager.class, Configuration.class})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MessagePersistManagerTest {

    @Autowired
    private MessagePersistManager messagePersistManager;

    @Autowired
    private TemplateManager templateManager;

    @Autowired
    private FileOperationsManager fileOperationsManager;

    @Autowired
    private Configuration freemarkerConfiguration;


    @Value("${channel.outbound.ftp.local.directory}")
    private String sepaFilesBasePath;

    @BeforeAll
    private void init() throws Exception {

        System.out.println("Creating Base Dir=" + sepaFilesBasePath);
        Files.createDirectories(Paths.get(sepaFilesBasePath));

        /* FreeMarker Configuration */
        freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), "/templates/");

    }


    @AfterAll
    private void destroy() throws Exception {

        System.out.println("Deleting Base Dir=" + sepaFilesBasePath);
        FileUtils.deleteDirectory(new File(sepaFilesBasePath));

    }


    @Test
    void persistSepaFile() {
        messagePersistManager.persistSepaFile("sepaWinnings.xml", generateData());
        System.out.println("e");
        assert (true);
    }

答案 2 :(得分:2)

答案 3 :(得分:0)

我遇到了同样的问题,因为我的ServletInitializer在不同的包中。纠正包结构后问题得到解决。