我有一个服务类,它调用Repository方法并返回一个List。现在我要模拟它。我的单元测试场景如下:
我的存储库类:
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类中获取列表,它应该返回测试类中创建的假对象。
答案 0 :(得分:1)
我相信您要寻找的是下面的内容。您无需执行正常的工作流程即可使用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();
}
}