我有一个服务类,该类调用存储库方法并返回列表。现在我想嘲笑它。我的单元测试方案将如下:
- 向模拟存储库添加一些模拟对象
- 查询服务类,返回模拟对象的列表
- 断言列表大小
我的存储库类:
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
public interface FoodRepository extends CrudRepository<Food, Long> {
@Query(value = "SELECT * FROM FOOD WHERE FRESH = 1", nativeQuery = true)
public List<Food> getMostFreshestFoods();
}
我的服务课:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class FoodService {
@Autowired
private FoodRepository foodRepository;
public List<Food> getMostFreshestFoods() {
return foodRepository.getMostFreshestFoods();
}
}
我的对象类:
public class Food {
private Long id;
private String foodName;
private boolean fresh;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public boolean isFresh() {
return fresh;
}
public void setFresh(boolean fresh) {
this.fresh = fresh;
}
}
和我的测试课:
import myapp.Food;
import myapp.FoodRepository;
import myapp.FoodService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class FoodTest {
@Mock
private FoodRepository foodRepository;
@InjectMocks
private FoodService foodService;
@Captor
private ArgumentCaptor<Food> foodArgumentCaptor;
@Test
public void testFreshestFoods() {
Food food = new Food();
food.setFoodName("Meat");
food.setFresh(true);
foodRepository.save(food);
verify(foodRepository).save(foodArgumentCaptor.capture());
assertThat(foodArgumentCaptor.getValue().getId(), is(notNullValue()));
// Above I added mock data to database to query list from Service class method,
// but I do not know how to do it. Using return always gives error
// I want to do the following: Query the list from FoodService class and it should
// return size 1 (the fake object (added above) )
}
}
,但是由于我是Mockito的新手,所以对我来说有点困难。我想知道如何从FoodService类中获取列表,它应该返回测试类中制作的假对象。
我相信您要寻找的是下面。您无需进行普通工作流程即可通过Mockito进行测试。另一种说法是您不会在数据库中添加物品,然后期望将它们恢复原状。Mockito不知道数据库。它接管了一堂课(嘲笑的人),并返回您告诉它的东西或抛出异常,或者什么也不做。这个想法是隔离正在测试的类。因此,就您而言,假装数据位于存储库中,只是模拟返回它。希望那是有道理的。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.List;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class FoodTest {
@Mock
private FoodRepository foodRepository;
@InjectMocks
private FoodService foodService;
@Captor
private ArgumentCaptor<Food> foodArgumentCaptor;
@Test
public void testFreshestFoods() {
Food food = new Food();
food.setFoodName("Meat");
food.setFresh(true);
// not needed
//foodRepository.save(food);
//verify(foodRepository).save(foodArgumentCaptor.capture());
//assertThat(foodArgumentCaptor.getValue().getId(), is(notNullValue()));
when(foodRepository.getMostFreshestFoods()).thenReturn(asList(food));
List<Food> actual = foodService.getMostFreshestFoods();
assertEquals(food, actual.get(0));
verify(foodRepository).getMostFreshestFoods();
}
}