Java单元测试


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) {

}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值