Web项目中的web方法测试--junit
1.首先继承基础测试类
package cn.maitian.maimai.test.search; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import cn.maitian.maimai.bi.es.service.StatExcellentIService; import cn.maitian.maimai.bi.es.service.SyncDateToEsService; import cn.maitian.maimai.bi.es.utils.FileUtils; import cn.maitian.maimai.bi.es.vo.DmCustomer; import cn.maitian.maimai.bi.es.vo.ESContant; import cn.maitian.maimai.core.util.AppPropUtil; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import cn.maitian.maimai.search.common.EsQueryModel; import cn.maitian.maimai.search.service.SearchService; import cn.maitian.maimai.test.base.AbstractServiceTest; import org.springframework.test.annotation.Rollback; import com.google.common.collect.Lists; /** * 测试搜索 * @author huang * */ public class SearchTest extends AbstractServiceTest{ @Autowired private JdbcTemplate jdbcTemplate; @Resource private SearchService searchService; @Autowired private StatExcellentIService statExcellentIService; @Autowired private SyncDateToEsService syncDateToEsService; @Test public void initDmCustomerData() { syncDateToEsService.syncDmCustomerData(); } @Test @Rollback(false) public void TestSearch() { statExcellentIService.calcExcellent("1", "2"); } @Test @Rollback(false) public void createIndex() { boolean flag = searchService.isIndexExists(AppPropUtil.getPropValue(AppPropUtil.Keys.BI_CLUSTER_NAME), "bi_kanyu"); System.out.println(flag); // flag = searchService.createIndex(AppPropUtil.getPropValue(AppPropUtil.Keys.BI_CLUSTER_NAME),"bi_kanyu"); // System.out.println(flag); flag = searchService.createMapping(AppPropUtil.getPropValue(AppPropUtil.Keys.BI_CLUSTER_NAME), "bi_kanyu","dm_housebase", FileUtils.getContentByLine("mappings/dm_housebase.txt")); System.out.println(flag); long starTime = System.currentTimeMillis(); flag = searchService.deleteIndex(AppPropUtil.getPropValue(AppPropUtil.Keys.BI_CLUSTER_NAME),"bi_kanyu"); System.out.println(flag); long endTime = System.currentTimeMillis(); System.out.println("耗时:"+(endTime-starTime)+"毫秒"); flag = searchService.isIndexTypeExists(AppPropUtil.getPropValue(AppPropUtil.Keys.BI_CLUSTER_NAME), "bi_kanyu","dm_customer"); System.out.println(flag); } @Test public void TestDelete() { DmCustomer dmcustomer = new DmCustomer(); dmcustomer.setMid("2FA32FAB1429FD325E014AC83348B9C3"); DmCustomer dmcustomer1 = new DmCustomer(); dmcustomer1.setMid("66995A152CDCDE626F71F8A8385245AF"); DmCustomer dmcustomer2 = new DmCustomer(); dmcustomer2.setMid("A31A372A6DA731B3238A5BC04E0440F0"); DmCustomer dmcustomer3 = new DmCustomer(); dmcustomer3.setMid("74748E8DFB847F5C2A02637E15FB4426"); DmCustomer dmcustomer4 = new DmCustomer(); dmcustomer4.setMid("F8BCFDDB67E74D63CF2253534A0DFB88"); DmCustomer dmcustomer5 = new DmCustomer(); dmcustomer5.setMid("74748E8DFB847F5C2A02637E15FB4426"); DmCustomer dmcustomer6= new DmCustomer(); dmcustomer6.setMid("F8BCFDDB67E74D63CF2253534A0DFB88aasaaaa"); DmCustomer dmcustomer7 = new DmCustomer(); dmcustomer7.setMid("1BDD4B4FDF2F50781DE0798A9852269Dsssssss"); List<DmCustomer> dmCustomers = Lists.newArrayList(dmcustomer, dmcustomer1, dmcustomer2,dmcustomer3,dmcustomer4,dmcustomer5,dmcustomer6,dmcustomer7); //es插入失败,强制回滚事物,干掉这一批插入的全部数据 logger.error("------------------batch插入es报错,强制回滚事物,批量删除开始------------------------"); StringBuffer buffer = new StringBuffer(); for(DmCustomer customer:dmCustomers) { buffer.append(customer.getMid()).append(","); } String midsString = buffer.substring(0, buffer.length()-1); Map<String, Object> conditions = new HashMap<String, Object>(); conditions.put("mid", midsString); searchService.deleteRecordsByConditions("maimai-es-test-24", ESContant.BI_KANYU_INDEX, ESContant.DM_CUSTOMER_TYPE, conditions); logger.error("------------------batch插入es报错,强制回滚事物,批量删除结束------------------------"); } }
2.AbstractServiceTest(在基础测试类中引入spring等基础配置类)
package cn.maitian.maimai.test.base; import javax.annotation.Resource; import javax.sql.DataSource; import org.apache.log4j.Logger; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; /** * 测试基础类(引入配置文件) * * @author LXINXIN * @company MAITIAN * @version 1.0 */ @ContextConfiguration(locations = { "/modules/spring.xml", "/modules/spring-ds-base-dev.xml", "/modules/spring-ds-stat-dev.xml", "/modules/spring-config-i18n.xml", "/modules/**/spring-conf/*.xml", "/modules/spring-config-cxf.xml" }) public abstract class AbstractServiceTest extends AbstractTransactionalJUnit4SpringContextTests { /** * 日志 */ protected final Logger logger = Logger.getLogger(getClass()); /** * 管理员ID */ protected static final String ADMIN_ID = "3C7DF024AE1C48AEAF15E6930F465F28"; /** * 行内分割 */ protected static final String SPLIT_IN_LINE = "@@"; /** * 行外风格 */ protected static final String SPLIT_OUT_LINE = "######"; @Override @Resource(name = "dataSource") public void setDataSource(DataSource dataSource) { super.setDataSource(dataSource); } }
划船不用桨、杨帆不等风、一生全靠浪