Java单元测试
程序员文章站
2022-03-04 07:51:39
...
package com.alipay.morderprod.biz.order.air.impl;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Field;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.alipay.acctrans.core.module.enums.AccountTypeEnum;
import com.alipay.airmng.common.service.facade.account.enums.AirAccountTypeEnum;
import com.alipay.airmng.common.service.facade.account.result.AirAccountServiceResult;
import com.alipay.morderprod.biz.order.product.SpecialProductManager;
import com.alipay.morderprod.biz.shared.enums.SpecialProductEnum;
import com.alipay.morderprod.common.service.integration.air.AirClient;
@RunWith(JMock.class)
public class AirManagerImplTest {
private Mockery context = new JUnit4Mockery() {
{
setImposteriser(ClassImposteriser.INSTANCE);
}
};
protected AirManagerImpl airManagerImpl = new AirManagerImpl();
//dependence class
AirClient airClient = context.mock(AirClient.class);
SpecialProductManager specialProductManager = context.mock(SpecialProductManager.class);
@Before
public void setUp() throws Exception {
airManagerImpl.setAirClient(airClient);
airManagerImpl.setSpecialProductManager(specialProductManager);
autoInject(airManagerImpl, specialProductManager, "specialProductManager");
}
/**
* 通过注解的方式注入对象.
*
* @param dest
* @param obj 需要注入的接口对象.
* @param name 需要注入的接口对象的名称.
*/
private void autoInject(Object dest, Object obj, String name) {
try {
Field f = dest.getClass().getDeclaredField(name);
f.setAccessible(true);//设置他能够访问private的对象.
f.set(dest, obj); //注入obj接口对象到dest对象中
} catch (Exception e) {
throw new RuntimeException("cannot set field by name:" + name, e);
}
}
@After
public void tearDown() throws Throwable {
context.assertIsSatisfied();
}
@Test
public void test_checkMainAccount() throws Throwable {
context.checking(new Expectations() {
{
SpecialProductEnum first = SpecialProductEnum.AIR_PERSONAL;
allowing(specialProductManager).getSpecialProduct(with(any(String.class)));
will(returnValue(first));
}
});
context.checking(new Expectations() {
{
AirAccountServiceResult first = new AirAccountServiceResult();
first.setSuccess(true);
allowing(airClient).checkMainAccount(with(any(String.class)),
with(any(AirAccountTypeEnum.class)));
will(returnValue(first));
}
});
String productId = "23123213123";
String mainCardNo = "23213213123";
AccountTypeEnum usertype = AccountTypeEnum.PRIVATE_ACCOUNT;
boolean result = airManagerImpl.checkMainAccount(productId, mainCardNo, usertype);
assertTrue(result);
}
@Test
public void test_checkMainAccount_1() throws Throwable {
context.checking(new Expectations() {
{
SpecialProductEnum first = SpecialProductEnum.AIR_PRO;
allowing(specialProductManager).getSpecialProduct(with(any(String.class)));
will(returnValue(first));
}
});
context.checking(new Expectations() {
{
AirAccountServiceResult first = new AirAccountServiceResult();
first.setSuccess(true);
allowing(airClient).checkMainAccount(with(any(String.class)),
with(any(AirAccountTypeEnum.class)));
will(returnValue(first));
}
});
String productId = "23123213123";
String mainCardNo = "23213213123";
AccountTypeEnum usertype = AccountTypeEnum.CORPORATE_ACCOUNT;
boolean result = airManagerImpl.checkMainAccount(productId, mainCardNo, usertype);
assertTrue(result);
}
@Test
public void test_checkMainAccount_2() throws Throwable {
context.checking(new Expectations() {
{
SpecialProductEnum first = SpecialProductEnum.AIR_PERSONAL;
allowing(specialProductManager).getSpecialProduct(with(any(String.class)));
will(returnValue(first));
}
});
context.checking(new Expectations() {
{
AirAccountServiceResult first = new AirAccountServiceResult();
first.setSuccess(false);
allowing(airClient).checkMainAccount(with(any(String.class)),
with(any(AirAccountTypeEnum.class)));
will(returnValue(first));
}
});
try {
String productId = "23123213123";
String mainCardNo = "23213213123";
AccountTypeEnum usertype = AccountTypeEnum.PRIVATE_ACCOUNT;
boolean result = airManagerImpl.checkMainAccount(productId, mainCardNo, usertype);
assertTrue(result);
} catch (Exception e) {
}
}
@Test
public void test_checkMainAccount_3() throws Throwable {
context.checking(new Expectations() {
{
SpecialProductEnum first = SpecialProductEnum.AIR_PRO;
allowing(specialProductManager).getSpecialProduct(with(any(String.class)));
will(returnValue(first));
}
});
context.checking(new Expectations() {
{
AirAccountServiceResult first = new AirAccountServiceResult();
first.setSuccess(false);
allowing(airClient).checkMainAccount(with(any(String.class)),
with(any(AirAccountTypeEnum.class)));
will(returnValue(first));
}
});
try {
String productId = "23123213123";
String mainCardNo = "23213213123";
AccountTypeEnum usertype = AccountTypeEnum.PRIVATE_ACCOUNT;
boolean result = airManagerImpl.checkMainAccount(productId, mainCardNo, usertype);
assertTrue(result);
} catch (Exception e) {
}
}
@Test
public void test_checkMainAccount_4() throws Throwable {
context.checking(new Expectations() {
{
allowing(specialProductManager).getSpecialProduct(with(any(String.class)));
will(throwException(new Exception()));
}
});
context.checking(new Expectations() {
{
AirAccountServiceResult first = new AirAccountServiceResult();
first.setSuccess(true);
allowing(airClient).checkMainAccount(with(any(String.class)),
with(any(AirAccountTypeEnum.class)));
will(returnValue(first));
}
});
try {
String productId = "";
String mainCardNo = "";
AccountTypeEnum usertype = AccountTypeEnum.PRIVATE_ACCOUNT;
boolean result = airManagerImpl.checkMainAccount(productId, mainCardNo, usertype);
assertTrue(result);
} catch (Exception e) {
}
}
}