码迷,mamicode.com
首页 > 编程语言 > 详细

Spring错误——Spring 单元测试——Test class should have exactly one public constructor

时间:2019-02-25 16:43:57      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:next   spring   class   extends   end   tor   span   junit4   on()   

  • 背景:Spring 构建单元测试
  • 错误
    java.lang.Exception: Test class should have exactly one public constructor
    
        at org.junit.runners.BlockJUnit4ClassRunner.validateOnlyOneConstructor(BlockJUnit4ClassRunner.java:131)
        at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:120)
        at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:101)
        at org.junit.runners.ParentRunner.validate(ParentRunner.java:344)
        at org.junit.runners.ParentRunner.<init>(ParentRunner.java:74)
        at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:55)
        at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
  • 错误原因:在Junit测试类中必须声明一个public 构造函数。我的测试类没有声明public,默认为default 。
    public class TestBeanScope extends UnitTestBase {
    
        TestBeanScope(){
            super("classpath*:spring-scope.xml");
        }
    
        @Test
        public void  TestSingleton(){
            for (int i=0;i<3;i++){
                BeanScope beanScope = (BeanScope)super.getBean("beanScope");
                beanScope.say("test");
            }
        }
    }
  • 解决办法:构建一个公有构造函数即可
    public class TestBeanScope extends UnitTestBase {
    
        public TestBeanScope(){
            super("classpath*:spring-scope.xml");
        }
    
        @Test
        public void  TestSingleton(){
            for (int i=0;i<3;i++){
                BeanScope beanScope = (BeanScope)super.getBean("beanScope");
                beanScope.say("test");
            }
        }
    }
  • Next

Spring错误——Spring 单元测试——Test class should have exactly one public constructor

标签:next   spring   class   extends   end   tor   span   junit4   on()   

原文地址:https://www.cnblogs.com/jixue/p/10431528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!