[英]How to test business logic in method?
我的服務層中有這樣的方法:
public Long calculateItemsCostInShoppingCart(Long shoppingCartId) {
List<Item> items = shoppingCartRepository.findAllItems(shoppingCartId);
Long cost = 0L;
for (Item item : items) {
cost += item.getPrice();
}
return cost;
}
我需要測試列表中所有項目的匯總成本計算。 我曾想到過模仿,但是並沒有解決,因為模仿只是創建存根,我需要真實的入口數據和基於它們的結果。 怎么辦
// create mock
ShoppingRepository mock = mock(ShoppingRepository.class);
// define return value for method findAllItems()
when(mock.findAllItems()).thenReturn(listOf(...));
假設您正在開發在應用程序服務器上運行的Java Web應用程序,另一種選擇是使用Arquillian( http://arquillian.org/ )。 簡而言之,Arquillian是一個框架,允許您在將要運行的環境中測試邏輯。 但是將Arquillian集成到您的項目中可能需要做一些工作。 我們正在多個項目中使用Arquillian,到目前為止效果很好。 即使是Alpha版本的Persistence模塊也可以正常工作。
這是一個如何使用Mockito進行測試的示例:
public class SomeCalculatorTest {
@Mock
private ShoppingCartRepository shoppingCartRepository;
@InjectMocks
private SomeCalculator someCalculator = new SomeCalculator();
@Before
public void setUp() {
initMocks(this);
}
@Test
public void testEmptyItemsList() {
when(shoppingCartRepository.findAllItems(any())).thenReturn(new ArrayList<>());
final Long result = someCalculator.calculateItemsCostInShoppingCart(1L);
assertThat(result, is(0L));
}
@Test
public void testOneItemInList() {
when(shoppingCartRepository.findAllItems(any())).thenReturn(Arrays.asList(new ItemImpl(25L)));
final Long result = someCalculator.calculateItemsCostInShoppingCart(1L);
assertThat(result, is(25L));
}
@Test
public void testTwoItemInList() {
when(shoppingCartRepository.findAllItems(any())).thenReturn(Arrays.asList(new ItemImpl(25L), new ItemImpl(12L)));
final Long result = someCalculator.calculateItemsCostInShoppingCart(1L);
assertThat(result, is(37L));
}
}
我需要測試列表中所有項目的匯總成本計算。
在這種情況下,您必須隔離不執行任何計算的shoppingCartRepository
依賴項。
我需要真實的入場數據並根據它們得出結果。 怎么辦
它描述了集成測試。 在這種情況下,請勿使用Mockito。
進行單元測試:
您必須模擬依賴關系,並且還需要一種方法來在被測類的實例中對其進行設置。
構造函數通常是一種好方法(讓被測類MyService
調用):
public MyService(ShoppingCartRepository shoppingCartRepository){
this.shoppingCartRepository = shoppingCartRepository;
}
然后,在測試中,您應該模擬ShoppingCartRepository
,為findAllItems()
記錄一個行為,並將該模擬傳遞給MyService的構造函數。
@Mock
private ShoppingCartRepository shoppingCartRepositoryMock;
@Test
public void calculateItemsCostInShoppingCart(){
Long cartId = Long.valueOf(123);
// set the dependency
MyService myService = new MyService(shoppingCartRepositoryMock);
// create the mock
Mockito.when(shoppingCartRepositoryMock.findAllItems(cartId))
.thenReturn(createMockedItems());
//action
Long actualCost = myService.calculateItemsCostInShoppingCart(cartId);
// assertion
Assert.assertEquals(expectedCost, actualCost);
}
private List<Item> createMockedItems() { ... }
您可以使用放心庫進行測試
獲取放心響應對象,並調用列表的方法對象的方法。
@BeforeClass
public static void init() {
RestAssured.baseURI = "http://localhost";
RestAssured.port = 8080;
}
@Test
public void testUserRegistration() {
Response response =
RestAssured
.given()
.get(URL_LOGIN)
.then()
.assertThat()
.statusCode(200);
Assert.assertThat(200, Matchers.is(200));
Assert.assertThat(response.getStatusCode(), Matchers.is(200));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.