springboot maven "lib"模块的单元测试

18 篇文章 0 订阅
9 篇文章 0 订阅

       由于在springboot maven的项目中分了模块,对于dao层模块来说,它没有@SpringBootApplication修饰的启动类,也就是说这个模块是没有spring ApplicationContext的,在单元测试的时候,报了错误:


java.lang.IllegalStateException: Failed to load ApplicationContext

	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
	at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
	at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
...

Process finished with exit code 255

解决方式:
步骤1.在dao模块的test/java下面的包中建一个用于测试的Application启动类


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @description:  由于dao模块没有SpringBootApplication的入口类,在这儿添加一个,
 * 否则单元测试的时候会报错"Failed to load ApplicationContext"
 * @author: pilaf
 * @create: 2019-03-28 18:43
 */
@SpringBootApplication
@MapperScan("com.**.dao.mapper")
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

步骤2.在单元测试类中创建H2数据源,用于单元测试,单元测试类代码如下:


import com.**.TestApplication;
import com.**.common.TestHelper;
import com.**.entity.PilafLog;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import javax.sql.DataSource;

/**
 * @description: 对MyBatis Mapper接口类的单元测试
 * @author: pilaf
 * @create: 2019-03-28 19:26
 */
@ActiveProfiles("ut")//对应application-ut.yml或者application-ut.properties文件
@SpringBootTest(classes = { TestApplication.class,PilafLogMapperUnitTest.class})
@RunWith(SpringRunner.class)
@Configuration
public class PilafLogMapperUnitTest {

    @Autowired
    private PilafLogMapper mapper;

    /**
     * 创建一个datasource对象,放到spring IoC容器中,这样就不会访问真实的数据库,而访问H2数据库
     * @return
     * @throws IOException
     */
    @Bean
    public DataSource dataSource() throws IOException {
		//TestHelper类代码在后面
        return TestHelper.generateH2DataSource("test_insert.sql");
    }

	//单元测试方法
    @Test
    public void testGet(){
    	//因为在用例sql语句中往PilafLog表中插入了一条主键为1的记录,所以这儿可以查到
        PilafLog PilafLog = mapper.selectByPrimaryKey(1L);
        Assert.assertEquals(1L,PilafLog.getId().longValue());
    }
}




import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import java.io.IOException;

/**
 *
 * 单元测试帮助类,用于将resources/db目录下的sql文件执行到H2数据库中
 * schema目录下是表结构语句,case目录下是单元测试用例的数据准备sql
 *
 */
@Slf4j
public class TestHelper {

	//caseSqlFileName是用于单元测试准备数据的sql文件名字
    public static EmbeddedDatabase generateH2DataSource(String caseSqlFileName) throws IOException {
    	//dao模块的test/resources/db/schema目录下放着表的建表语句
        String locationPattern = "classpath:db/schema/*.sql";
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(new PathMatchingResourcePatternResolver());
        Resource[] resources = resolver.getResources(locationPattern);
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        builder.setType(EmbeddedDatabaseType.H2);
        builder.setName("testdb;MODE=MySQL");
        for (Resource resource : resources) {
            builder.addScript("db/schema/" + resource.getFilename());
        }
        if (StringUtils.isNotBlank(caseSqlFileName)) {
            builder.addScript("db/case/" + caseSqlFileName);
        }
        return builder.build();
    }

}

补充说明,如果对MyBatis的Mapper类进行单元测试,还需要在application-ut.yml(因为@ActiveProfiles中要指定环境,这儿都是ut,即unit test的意思)或application-ut.properties中指定mybatis的mapper-locations,这儿以yml文件为例:

mybatis:
    mapper-locations: classpath:mapper/*.xml
    type-aliases-package: com.**.dao.entity
    configuration:
      map-underscore-to-camel-case: true

参考网址:
1.https://stackoverflow.com/questions/37615914/how-to-test-maven-module-project-with-spring-boot

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值