JMockit書き込みユニットでテスト
テキストの紹介:
Mock
テストは一般的なテスト方法です.通常、テストを実行するとき、テストコードは、いくつかの実際のオブジェクトと対話する必要があるか、または被測定コードの実行は、実際のオブジェクトの機能に依存する必要がある.この場合、軽量レベルの制御可能な
Mock
オブジェクトは実際のオブジェクトに取って代わって、実際のオブジェクトの動作と機能をシミュレートして、私たちがテストするのに便利です.
jMock
この方法の実現である.
JMockは、Mockオブジェクトを利用してJavaコードをテストする軽量レベルのテストツールです.例外なくxUnitファミリーの一員でもあり、JUnitから発展したため、JUnitの強化ライブラリです.jMockの使い方は簡単で、把握しやすい.これにより、必要なMockオブジェクトを簡単に迅速に構築でき、ユニットテストコードを簡単に作成でき、テスト駆動開発のスムーズな進行に適しています.
私が出会った例は、上のコードです。
public class InnerCustomerInfoServiceTest extends BaseServiceTest {
@SpringBeanByType
InnerCustomerInfoService innerCustomerInfoService;// spring bean
//...
/**
* NULL
*/
@Test
public void test1_FindCustomerInfo() {
new MockUp<IbatisCustomerInfoDAO>() {// 1
@Mock
CustomerInfoDO findCustomerInfo(long customerId) {
want.number(customerId).isEqualTo(2);
return null;
}
};
CustomerInfoDO entity = innerCustomerInfoService.findCustomerInfo(2);// , 1
Assert.assertTrue(entity == null);
}
/**
*
*/
@Test
public void test2_findCustomerInfoByKpId() {
new MockUp<IbatisCustomerInfoDAO>() {
@Mock
CustomerInfoDO findCustomerInfoByKpId(long kpId) {
want.number(kpId).isEqualTo(1);
CustomerInfoDO tmpDO = new CustomerInfoDO();
tmpDO.setCustomerRank(2);
return tmpDO;
}
};
CustomerInfoDO entity = innerCustomerInfoService.findCustomerInfoByKpId(1);
want.number(entity.getCustomerRank()).isEqualTo(2);// 2
}
/**
*
*/
@Test
public void test3_findCustomerInfoByKpId() {
new MockUp<IbatisCustomerInfoDAO>() {
@Mock
CustomerInfoDO findCustomerInfoByKpId(long kpId) {
want.number(kpId).isEqualTo(1);
return null;
}
};
new MockUp<InnerCustomerInfoServiceImpl>() {// 3
@Mock
CustomerInfoDO createNewCustomerInfoByKpId(long kpId) {
want.number(kpId).isEqualTo(1);
CustomerInfoDO tmpDO = new CustomerInfoDO();
tmpDO.setCustomerRank(2);
return tmpDO;
}
};
CustomerInfoDO entity = innerCustomerInfoService.findCustomerInfoByKpId(1);
want.number(entity.getCustomerRank()).isEqualTo(2);
}
//...
}
説明:
1.new MockUp
public class InnerCustomerInfoServiceImpl implements InnerCustomerInfoService {
private static final Log log = LogFactory.getLog(InnerCustomerInfoServiceImpl.class);
@Autowired
private CustomerInfoDAO customerInfoDAO;
@Override
public CustomerInfoDO findCustomerInfo(long customerId) {
if (customerId <= 0) {
log.error("param error,customerId is: " + customerId);
return null;
}
return customerInfoDAO.findCustomerInfo(customerId);// CustomerInfoDAO , ,
// 1 IbatisCustomerInfoDAO
}
//...
}
2.wantの意味は?上のAssertを考えてみましょうassertTrue..
3.mock 2個?説明は2つのオブジェクトを使用し、2つのオブジェクトをシミュレートする必要があります.具体的なコードと説明1は同じです.実はとても簡単で、ひょうたんを見てひょうたんを描くだけでいいです!
4.JMockでインポートするJarパッケージについて?この例ではすでにMavenと同じ構成で管理されており、jtesterではpom.xm関連部分は下図の通りです.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>com.NB.jtester</groupId>
<artifactId>jtester.junit</artifactId>
<version>${jtester_version}</version>
</dependency>
<dependency>
<groupId>com.NB.jtester</groupId>
<artifactId>jtester.integrated</artifactId>
<version>${jtester_version}</version>
</dependency>
<dependency>
<groupId>com.NB.jtester</groupId>
<artifactId>jtester.spec</artifactId>
<version>${jtester_version}</version>
</dependency>
公式住所:
http://jmock.org/