Junit4+springmvc 单元测试01

测试的整体代码如下:

package com.zykj.modules.web;

import javax.servlet.http.HttpServletRequest;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.zykj.common.config.Settings;
import com.zykj.common.config.utils.InitSettings;
import com.zykj.common.utils.JedisUtils;
import com.zykj.modules.entity.renting.RentMember;
import com.zykj.modules.entity.sys.Member;
import com.zykj.modules.utils.MemberUtils;
import com.zykj.utils.lang.StringUtils;

//让测试运行于Spring环境
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
// 引入Spring配置
@ContextConfiguration({ "classpath*:/spring-context.xml", "classpath*:/spring-mvc.xml",
		"classpath*:/spring-context-jedis.xml", "classpath*:/spring-context-shiro.xml" })
public class JdeisUtilsTest {

	@Before
	public void setUp() throws Exception {
		InitSettings.initSettings("settings-global.properties", "settings-custom.properties");

	}

	@Test
	public void testJedisUtils() {

		try {
			Integer cacheSeconds = Integer.parseInt(Settings.getConfig("redisCacheSeconds"));
			RentMember rentMember = new RentMember();
			rentMember.setId("123456");
			rentMember.setMemberToken("token123456");
			JedisUtils.setObject("rentMember_" + rentMember.getId(), rentMember, cacheSeconds);
			System.out.println("jedisUtils测试成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



}

 

上述代码,是测试redis工具是否正常,不是严谨的junit测试代码。

使用途中出现的错误如下:

1、java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig

解决办法:

maven添加依赖


       org.apache.geronimo.specs
       geronimo-servlet_3.0_spec
       1.0
       test

2、Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
因为刚开始没有加下图红框注解,所报的错,解决办法,加上就好了

Junit4+springmvc 单元测试01_第1张图片

你可能感兴趣的:(java,junit)