【关于使用SpringJUnit4ClassRunner单元测试报错问题】

今天单元测试如下的代码的时候发现项目老是报错:

package com.yhb.jsxn.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.yhb.jsxn.entity.AccountsPastTable;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext*.xml"})
public class TestService {
    protected static final Logger LOG = LoggerFactory.getLogger(TestService.class);
    @Autowired
    private AccountsPastTableService accountsPastTableService;
    @Test
    public void test() {
        HashMap<String, String> parms = new HashMap<String,String>();
        parms.put("userId", "BB7534E3-4F35-4FF1-B930-CCC962C45DEF");
        List<AccountsPastTable> list = accountsPastTableService.selectByUserIdAndTime(parms);//升序排列
        int countDay = 0;//连续天数
        int times = 0;//抽奖次数
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < list.size() - 1; i++) {
            calendar.setTime(list.get(i).getPastTime());
            int before = calendar.get(Calendar.DAY_OF_YEAR);
            calendar.setTime(list.get(i+1).getPastTime());
            int after = calendar.get(Calendar.DAY_OF_YEAR);
            if ((after - before) == 1) {
                countDay++;
                if (countDay == 3) {
                    times++;
                    countDay-=3;
                }
            }else {
                countDay = 0;
            }
        }
        System.out.println("可抽奖次数" + times);        

    }

}

统计抽奖次数

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\temp\p2p_yhb\build\classes\com\yhb\jsxn\apppush\android\AndroidBroadcast.class]; nested exception is org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn‘t supported yet: file [C:\temp\p2p_yhb\build\classes\com\yhb\jsxn\apppush\android\AndroidBroadcast.class]; nested exception is java.lang.IllegalArgumentException
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:290)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242)
at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1438)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:185)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:139)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:108)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:233)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:117)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 25 more
Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn‘t supported yet: file [C:\temp\p2p_yhb\build\classes\com\yhb\jsxn\apppush\android\AndroidBroadcast.class]; nested exception is java.lang.IllegalArgumentException
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:56)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:102)
at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:266)
... 48 more
Caused by: java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:53)
... 51 more

后来查找原因是:因为我前几天排序的时候使用了jdk8的Lambda表达式,把编译环境改成了1.8。spring 3.2不支持1.8编译环境,解决办法就是降为1.7编译环境 或者换成spring 3.1

时间: 2024-10-28 20:19:22

【关于使用SpringJUnit4ClassRunner单元测试报错问题】的相关文章

[转]method initializationerror not found:JUnit4单元测试报错问题

method initializationerror not found:JUnit4单元测试报错问题 原文地址:http://blog.csdn.net/chenleixing/article/details/44257839 2015-03-14  分类: Maven-JUnit-JProfiler等工具(14)  版权声明:本文为博主原创文章,未经博主允许不得转载. 今天使用JUnit 4进行单元测试时,测试程序一直运行不起来,报method initializationerror not

单元测试报错NoSuchBeanDefinitionException

问题 在SpringBoot项目开发时,使用单元测试来运行一个Service,发现调用的一个Bean就是找不到,报错为: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.gaosiedu.gsl.mq.base.MqSendService' available: expected at least 1 bean which qualifies as a

dao层单元测试报错CONDITIONS EVALUATION REPORT

0 环境 系统:win10 编辑器:IDEA 1 正文 1.1 起因 在controller层测试 测试url时没问题的 但是我单元测试就报错 1.2 排查 因为controller层 springbootapplication启动项目没问题 说明xml文件是没问题的 可能性很大的就是dao层没映射到 重点排查一下 打算debug的 突然想起来我在springbootapplication文件下添加了mappe包的包扫描 然而dao层的单元测试 没有启动springbootapplication

method initializationerror not found:JUnit4单元测试报错问题

今天使用JUnit 4进行单元测试时,测试程序一直运行不起来,报method initializationerror not found错误,如下: 网上说版本不对,我试了试,还是有这个问题,后来才知道,缺少了jar包,除了引入junit-4.12.jar之外,还要引入2个依赖jar包:hamcrest-core-1.3.rc2.jar,hamcrest-library-1.3.rc2.jar,提供大家免费的下载链接: junit-4.12.jar下载 hamcrest-library-1.3.

spring接入swagger后单元测试报错

2017-05-16 11:58:05.276 INFO 11268 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]58bf3596: startup date [Tue May 16 11:58:05 CST 2017]; root of context hierarchy 2017-05-16 11:58:06.452 INFO 11268 --- [

如何解决JUnit4单元测试报错 :method initializationerror not found

场景:用Junit4 运行测试时候,无法运行, 报method initializationerror not found.  看日志发现,是有重复的Feature  Step   Case冲突导致. 解决方案: 1.在解决代码冲突时候,需要确保冲突文件整理完毕,再Push 脚本代码,防止项目运行不成功. 2.去Feature文件把重复的Step删除即可.

【JUnit 报错】 method initializationerror not found:JUnit4单元测试报错问题

1.不仅方法要public,类也要是public权限 2.修改Java文件字符集 没有完成,决定修改eclipse的默认字符集为gdk,需要使用时再修改为utf-8 原文地址:https://www.cnblogs.com/thyHome/p/8905076.html

SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)

引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications @SpringBootTest(webEnvironment = SpringBootT

单元测试时候使用[ClassInitialize]会该方法必须是静态的公共方法,不返回值并且应采用一个TestContext类型的参数报错的解决办法

using Microsoft.VisualStudio.TestTools.UnitTesting; 如果该DLL应用的是 C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 版本为9.0.0.0 的话,则使用[ClassInitialize]会该方法必须是静态的公共方法,不返回值并且