java – 在spring boot中使用testNG进行单元测试需要时间来构建项目

栏目: Java · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/30480046/unit-test-with-testng-in-spring-boot-takes-time-to-build-project
我在 spring

-boot中创建了一个Web应用程序.我正在使用testNG为我的业务层编写单元测试.

我创建了Application类

@SpringBootApplication
public class TestApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(TestApplication.class, args);
    }

    @Bean
    Mapper mapper()
    {
        List<String> mappingFiles = new ArrayList<String>();
        mappingFiles.add("dozer-mappings.xml");
        return new DozerBeanMapper(mappingFiles);
    }
}

我的测试类看起来像

@ContextConfiguration(classes = { TestApplication.class })
public class CommissionRuleServiceTest extends AbstractTestNGSpringContextTests
{
     @InjectMocks
     @Autowired
     MyService

     @Mock
     MyDAO;

     @BeforeMethod
     public void initMock()
     {
          MockitoAnnotations.initMocks(this);
     }

     @Test(dataProvider = "....")
     ......
     ......
}

当我运行项目时,它会显示休息登录控制台,只需几次小测试就需要20.00秒.

日志中的一些语句是,

DEBUG o.s.c.i.s.PathMatchingResourcePatternResolver  Searching directory  DEBUG o.s.c.a.ConfigurationClassPostProcessor  DEBUG o.s.c.a.ClassPathBeanDefinitionScanner  DEBUG o.s.c.i.s.PathMatchingResourcePatternResolver  DEBUG o.s.b.f.s.DefaultListableBeanFactory  DEBUG o.a.c.b.converters.ArrayConverter  DEBUG org.dozer.loader.xml.XMLParser  DEBUG org.hibernate.cfg.SettingsFactory  DEBUG o.h.cfg.annotations.CollectionBinder  DEBUG o.h.cfg.annotations.TableBinder  DEBUG o.h.p.w.spi.MetamodelGraphWalker  Visiting attribute path : MyEntity  DEBUG o.s.b.f.s.DefaultListableBeanFactory  DEBUG org.hibernate.SQL

为什么要这么“休”时间?我该怎么办?

调查:

@SpringBootApplication注释等效于以下带有默认属性的注释:

> @Configuration – 表示该类包含一个或多个@Bean方法.与@ComponentScan一起播放.

> @EnableAutoConfiguration – 将尝试猜测并配置您可能需要的bean.根据您的应用程序,这可能会导致性能下降.

> @ComponentScan – 配置组件扫描.由于未定义包,因此将使用此注释从类的包中进行扫描.

如果没有更多的代码,就无法给出准确的猜测,但我认为大多数性能损失是由Spring Boot初始化引起的.

翻译自:https://stackoverflow.com/questions/30480046/unit-test-with-testng-in-spring-boot-takes-time-to-build-project


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Android 源码设计模式解析与实战

Android 源码设计模式解析与实战

何红辉、关爱民 / 人民邮电出版社 / 2015-11 / 79.00元

本书专门介绍Android源代码的设计模式,共26章,主要讲解面向对象的六大原则、主流的设计模式以及MVC和MVP模式。主要内容为:优化代码的首步、开闭原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特原则、单例模式、Builder模式、原型模式、工厂方法模式、抽象工厂模式、策略模式、状态模式、责任链模式、解释器模式、命令模式、观察者模式、备忘录模式、迭代器模式、模板方法模式、访问者模式、中介......一起来看看 《Android 源码设计模式解析与实战》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具