问下ShopServiceTest那个测试类
来源:
慕粉2302038000
2018-05-22
@AutoWired
Private ShopService shopService
Run JUnit Test以后,显示cannot create bean shop serviceTest
请问有遇到过的吗?
写回答
1回答
-
同学好,请问具体的信息是什么,应该是你的配置没配对,请按照视频的配置来
1.BaseTest里将两个xml文件给配置上
BaseTest.java
package com.imooc.o2o; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * 配置spring和junit整合,junit启动时加载springIOC容器 * */ @RunWith(SpringJUnit4ClassRunner.class) // 告诉junit spring配置文件的位置 @ContextConfiguration({ "classpath:spring/spring-dao.xml", "classpath:spring/spring-service.xml", "classpath:spring/spring-redis.xml" }) public class BaseTest { }
2.ShopServiceImpl头部记得加
@Service
标签
3.测试类记得写好,该加的标签加上
ShopServiceTest.java
package com.imooc.o2o.service; import static org.junit.Assert.assertEquals; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.Date; import org.junit.Ignore; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.imooc.o2o.BaseTest; import com.imooc.o2o.dto.ImageHolder; import com.imooc.o2o.dto.ShopExecution; import com.imooc.o2o.entity.Area; import com.imooc.o2o.entity.PersonInfo; import com.imooc.o2o.entity.Shop; import com.imooc.o2o.entity.ShopCategory; import com.imooc.o2o.enums.ShopStateEnum; import com.imooc.o2o.exceptions.ShopOperationException; public class ShopServiceTest extends BaseTest { @Autowired private ShopService shopService; @Test public void testGetShopList() { Shop shopCondition = new Shop(); ShopCategory sc = new ShopCategory(); sc.setShopCategoryId(3L); shopCondition.setShopCategory(sc); ShopExecution se = shopService.getShopList(shopCondition, 2, 2); System.out.println("店铺列表数为:" + se.getShopList().size()); System.out.println("店铺总数为:" + se.getCount()); } @Test @Ignore public void testModifyShop() throws ShopOperationException, FileNotFoundException { Shop shop = new Shop(); shop.setShopId(1L); shop.setShopName("修改后的店铺名称"); File shopImg = new File("/Users/baidu/work/image/dabai.jpg"); InputStream is = new FileInputStream(shopImg); ImageHolder imageHolder = new ImageHolder("dabai.jpg", is); ShopExecution shopExecution = shopService.modifyShop(shop, imageHolder); System.out.println("新的图片地址为:" + shopExecution.getShop().getShopImg()); } @Test @Ignore public void testAddShop() throws ShopOperationException, FileNotFoundException { Shop shop = new Shop(); PersonInfo owner = new PersonInfo(); Area area = new Area(); ShopCategory shopCategory = new ShopCategory(); owner.setUserId(1L); area.setAreaId(2); shopCategory.setShopCategoryId(1L); shop.setOwner(owner); shop.setArea(area); shop.setShopCategory(shopCategory); shop.setShopName("测试的店铺3"); shop.setShopDesc("test3"); shop.setShopAddr("test3"); shop.setPhone("test3"); shop.setCreateTime(new Date()); shop.setEnableStatus(ShopStateEnum.CHECK.getState()); shop.setAdvice("审核中"); File shopImg = new File("/Users/baidu/work/image/xiaohuangren.jpg"); InputStream is = new FileInputStream(shopImg); ImageHolder imageHolder = new ImageHolder(shopImg.getName(), is); ShopExecution se = shopService.addShop(shop, imageHolder ); assertEquals(ShopStateEnum.CHECK.getState(), se.getState()); } }
4.spring-service.xml文件配置也请检查一下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描service包下所有使用注解的类型 --> <context:component-scan base-package="com.imooc.o2o.service" /> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置基于注解的声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
总之,请同学仔细比对好视频,程序说1是1,不可能大家的行偏偏同学的不行,肯定是哪里写错了,请仔细检查,下次也请同学附上详细的信息方便定位问题
012018-05-25
相似问题