junit测试
/**
ssm框架测试service
**/ import com.alibaba.fastjson.JSON; import com.raycloud.waimai.customer.center.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; //表示继承了springJUnit4ClassRunner @RunWith(SpringJUnit4ClassRunner.class)
//加载配置文件 @ContextConfiguration(locations = "classpath:spring/spring-*.xml") public class UserServiceTest { //依赖注入 @Resource UserService userService; @Test public void testGetId(){ User user=userService.getUserByKey((long) 162233840); System.out.println(JSON.toJSON(user)); } }
测试dao层以及services
//表示继承了springJUnit4ClassRunner @RunWith(SpringJUnit4ClassRunner.class) //加载配置文件 @ContextConfiguration(locations = "classpath:spring/spring-*.xml") public class UserServiceTest { @Test public void testGetId(){ UserService userService=(UserService)SpringUtil.getBean("userService"); User user=userService.getUserByKey((long) 162233840); System.out.println(JSON.toJSON(user)); } }