• SpringBoot项目下的JUnit测试


    在SpringBoot项目里,要编写单元测试用例,需要依赖4个jar。一个是最基本的JUnit,然后是spring-test和spring-boot-test。

            <!--test-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.1.9.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>2.1.7.RELEASE</version>
                <scope>test</scope>
            </dependency>

    如果在运行testcase时,遇到如下问题,那么,原因是因为没有引入spring-core包(错误消息里说的很明白了,见其中的springframework/core)

    java.lang.NoSuchMethodError: org.springframework.core.type.AnnotationMetadata.introspect(Ljava/lang/Class;)Lorg/springframework/core/type/AnnotationMetadata;
    
        at org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition.<init>(AnnotatedGenericBeanDefinition.java:58)
        at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.doRegisterBean(AnnotatedBeanDefinitionReader.java:253)
        at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(AnnotatedBeanDefinitionReader.java:147)
        at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.register(AnnotatedBeanDefinitionReader.java:137)
        at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:156)
        at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:135)
        at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:127)
        at org.springframework.boot.SpringApplication.load(SpringApplication.java:687)
        at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:385)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
        at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
        at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
        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)

    依赖spring-core包:

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>

    如下是一个testcase示例。其中

    @RunWith是JUnit的一个运行器。@RunWith(JUnit4.class)就是指用JUnit4来运行;@RunWith(SpringJUnit4ClassRunner.class),让测试运行于Spring测试环境

    import com.alibaba.fastjson.JSON;
    import org.apache.dubbo.config.annotation.Reference;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class CarSignatureServiceTest {
    
        @Reference(url = "dubbo://localhost:20880")
        private CarSignatureService carSignatureService;
    
    
        @Test
        public void getSignedCarList(){
            List<ServiceOrderVO> list = carSignatureService.getSignedCarList(new ServiceOrderVO());
            System.out.println(JSON.toJSONString(list));
        }
    }
  • 相关阅读:
    你的C/C++程序为什么无法运行?揭秘Segmentation fault (2)
    亲,这就是遗传算法
    我们为什么需要Map-Reduce?
    搜索引擎-架构概述(2)
    搜索引擎-架构概述(1)
    单源最短路径-迪杰斯特拉算法(Dijkstra's algorithm)
    最小生成树-普利姆算法eager实现
    最小生成树-普利姆算法lazy实现
    最小生成树-克鲁斯卡尔算法(kruskal's algorithm)实现
    索引式优先队列(indexed priority queue)
  • 原文地址:https://www.cnblogs.com/buguge/p/11889207.html
Copyright © 2020-2023  润新知