Просмотр исходного кода

增加单元测试的配置支持以及基础类

suntg 7 лет назад
Родитель
Сommit
26b8ba9966

+ 12 - 0
pom.xml

@@ -47,6 +47,12 @@
         </profile>
     </profiles>
     <dependencies>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>javax.servlet-api</artifactId>
+            <version>3.0.1</version>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>javax.servlet</groupId>
             <artifactId>servlet-api</artifactId>
@@ -94,6 +100,12 @@
             <groupId>org.springframework.data</groupId>
             <artifactId>spring-data-jpa</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+            <version>4.1.6.RELEASE</version>
+            <scope>test</scope>
+        </dependency>
         <!-- -->
         <dependency>
             <groupId>commons-fileupload</groupId>

+ 39 - 0
src/test/java/com/uas/platform/b2b/BaseJunitTest.java

@@ -0,0 +1,39 @@
+package com.uas.platform.b2b;
+
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.transaction.TransactionConfiguration;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.context.WebApplicationContext;
+
+/**
+ * Spring MVC 测试基类,所有测试类继承自这个类就可以直接写单元测试
+ * @author stg
+ * @date 2017年11月8日16:48:18
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration("classpath:spring/*.xml")
+@WebAppConfiguration("")
+@Transactional
+@TransactionConfiguration(defaultRollback = true)
+public class BaseJunitTest {
+
+    @Autowired
+    protected WebApplicationContext wac;
+
+    protected MockMvc mockMvc;
+
+    /**
+     * 前置防范,配置MVCMock
+     */
+    @Before
+    public void setup() {
+        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
+    }
+}

+ 130 - 0
src/test/java/com/uas/platform/b2b/purc/PurchaseNotify.java

@@ -0,0 +1,130 @@
+package com.uas.platform.b2b.purc;
+
+import com.uas.platform.b2b.BaseJunitTest;
+import com.uas.platform.b2b.dao.PurchaseNoticeDao;
+import com.uas.platform.b2b.dao.PurchaseOrderItemDao;
+import com.uas.platform.b2b.model.PurchaseNotice;
+import com.uas.platform.b2b.model.PurchaseOrderItem;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.transaction.TransactionConfiguration;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.PersistenceContext;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+public class PurchaseNotify extends BaseJunitTest {
+
+    @Autowired
+    private PurchaseOrderItemDao purchaseOrderItemDao;
+
+    @Autowired
+    private PurchaseNoticeDao purchaseNoticeDao;
+
+    @Autowired
+    private EntityManagerFactory entityManagerFactory;
+
+    @PersistenceContext
+    protected EntityManager em;
+
+    @Transactional
+    public void batchInsert(List list) {
+        for(int i = 0; i < list.size(); i++) {
+            em.persist(list.get(i));
+            if(i % 30== 0) {
+                em.flush();
+                em.clear();
+            }
+        }
+    }
+
+    @Test
+    public void testFindTime() {
+        Long orderItemId = 37053424L;
+//        String orderCode = "B2BYBMP180100104";
+//        Long enuu = 10041166L;
+//        Short number = 1;
+
+        String orderCode = "130000443-73";
+        Long enuu = 10006098L;
+        Short number = 2;
+        long time1 = System.currentTimeMillis();
+        List<Long> orderItemIds = purchaseOrderItemDao.findIdByEnUUAndOrderCodeAndNumber(enuu, orderCode, number);
+        long time2 = System.currentTimeMillis();
+        List<PurchaseOrderItem> orderItems = purchaseOrderItemDao.findByEnUUAndOrderCodeAndNumber(enuu, orderCode, number);
+        long time3 = System.currentTimeMillis();
+        System.out.println("time1: " + (time2 - time1) + " , time2: " + (time3 - time2));
+    }
+
+    @Test
+    public void testSaveTime() {
+        String orderCode = "130000443-73";
+        Long enuu = 10006098L;
+        Short number = 2;
+        List<PurchaseNotice> purchaseNotices = new ArrayList<>();
+        for (int i = 0; i < 100; i ++) {
+            purchaseNotices.add(createNotice());
+        }
+
+        List<PurchaseNotice> purchaseNotices2 = new ArrayList<>();
+        for (int i = 0; i < 100; i ++) {
+            purchaseNotices2.add(createNotice());
+        }
+        long time1 = System.currentTimeMillis();
+        batchInsert(purchaseNotices);
+        long time2 = System.currentTimeMillis();
+        purchaseNoticeDao.save(purchaseNotices2);
+        long time3 = System.currentTimeMillis();
+        System.out.println("time1: " + (time2 - time1) + " , time2: " + (time3 - time2));
+    }
+
+    private void persistByEntityManager(Iterable<?> entities) {
+        EntityManager entityManager = null;
+        EntityTransaction transaction = null;
+
+        try {
+            entityManager = entityManagerFactory.createEntityManager();
+            transaction = entityManager.getTransaction();
+            transaction.begin();
+
+            Iterator iterator = entities.iterator();
+            while(iterator.hasNext()){
+                entityManager.persist(iterator.next());
+            }
+            transaction.commit();
+        } catch (RuntimeException e) {
+            if ( transaction != null &&
+                    transaction.isActive()) {
+                transaction.rollback();
+            }
+            throw e;
+        } finally {
+            if (entityManager != null) {
+                entityManager.close();
+            }
+        }
+
+    }
+
+    private PurchaseNotice createNotice() {
+        PurchaseNotice notice = new PurchaseNotice();
+        notice.setQty((double) 1);
+        notice.setDelivery(new Date());
+        notice.setStatus((short) 201);
+        notice.setSendStatus((short) 201);
+        notice.setEndQty((double) 0);
+        notice.setDate(new Date());
+        notice.setEnUU(10041166L);
+        notice.setErpDate(new Date());
+        notice.setVendUU(10030994L);
+        notice.setOrderItemId(37120085L);
+        return notice;
+    }
+
+}