Spring Test 是一个极简的测试框架
本次我们结合了Junit
- Maven引入Spring Test
- 创建一个配置类
package cn.derry.core.service;
import org.apache.log4j.Logger;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
/**
* spring 单元测试
* <p>
* MangoDai
* 2017 07 16 4:09 PM
*/
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@ContextConfiguration(locations = "classpath:application-context.xml")
@TestExecutionListeners(listeners = {
DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class}
)
@Transactional
public abstract class AbstractSpringJunit {
protected Logger logger = Logger.getLogger(this.getClass());
}
- 继承这个类
package cn.derry.core.service.impl;
import cn.derry.core.bean.TFix;
import cn.derry.core.service.AbstractSpringJunit;
import cn.derry.core.service.FixService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
* MangoDai
* 2017 09 14 5:21 PM
*/
public class FixServiceTest extends AbstractSpringJunit {
@Autowired
FixService fixService;
@Test
public void testGetAll() {
List<TFix> fixListByOrderID = fixService.getFixListByOrderID(1);
// fixListByOrderID.stream().forEach(i -> System.out.println(i));
for (TFix t : fixListByOrderID) {
System.out.println("t = " + t);
}
}
@Test
public void testAddTix() {
TFix tFix = new TFix();
tFix.setUserId(1);
tFix.setSaleId(1);
tFix.setOrderId(1);
tFix.setFixTotal(new BigDecimal(100.00));
tFix.setCreateTime(new Date());
tFix.setFixReason("5");
tFix.setFixContent("5");
tFix.setFixCallBack(new Date());
int i = fixService.addFix(tFix);
System.out.println(i);
System.out.println("tFix = " + tFix);
}
}