我开始学习模仿以测试我的课程。我知道如何使用带有一个(可能是2个)模拟的小类来做到这一点,但是当我的服务更大时,我遇到了一个问题。例如,我有服务
public class ShoppingListService {
Map<Ingredient, Long> shoppingList = new HashMap<>();
List<MealInfo> meals = new ArrayList<>();
UserInfoService userInfoService;
DietMealsService dietMealsService;
UserRepository userRepository;
User user;
@Autowired
public ShoppingListService(UserInfoService userInfoService, DietMealsService dietMealsService,UserRepository userRepository) {
this.userInfoService = userInfoService;
this.dietMealsService = dietMealsService;
this.userRepository = userRepository;
}
public Map<Ingredient,Long> createShoppingList(){
user = userRepository.findByLoginAndPassword(userInfoService.getUser().getLogin(),userInfoService.getUser().getPassword()).get();
shoppingList.clear();
meals.clear();
meals = user.getDiet().getMeals();
meals=dietMealsService.adjustIngredients(meals);
for (MealInfo meal : meals) {
meal.getMeal().getIngredients().forEach(s -> {
if(shoppingList.containsKey(s.getIngredient()))
shoppingList.put(s.getIngredient(), s.getWeight()+shoppingList.get(s.getIngredient()));
else
shoppingList.put(s.getIngredient(),s.getWeight());
});
}
return shoppingList;
}
}
我想测试方法createShoppingList
。
我应该创建几个实例并模拟每个字段(除了shoppingList和餐点之外),然后在使用时创建1或2个食材,餐点和使用后实例吗->像这样吗?
@Test
public void createShoppingList() {
//GIVEN
Ingredient pineapple = new Ingredient().builder().name("Pineapple").caloriesPer100g(54F).carbohydratePer100g(13.6F).fatPer100g(0.2F).proteinPer100g(0.8F).build();
Ingredient watermelon = new Ingredient().builder().name("Watermelon").caloriesPer100g(36F).carbohydratePer100g(8.4F).fatPer100g(0.1F).proteinPer100g(0.6F).build();
IngredientWeight pineappleWithWeight...
//after this create Meal, MealInfo, Diet...
}
在其他班级以下:
public class MealInfo implements Comparable<MealInfo>{
@Id
@GeneratedValue
private Long id;
private LocalDate date;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "meal_id")
private Meal meal;
private String name;
@ManyToMany(cascade = CascadeType.REMOVE)
@JoinTable(name = "diet_meal_info", joinColumns = @JoinColumn(name = "meal_info_id"),
inverseJoinColumns = @JoinColumn(name = "diet_id"))
private List<Diet> diet;
public MealInfo(LocalDate date, String description, Meal meal) {
this.date = date;
this.name = description;
this.meal = meal;
}
@Override
public int compareTo(MealInfo o) {
return getName().compareTo(o.getName());
}
}
public class Meal {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "meal_ingredient", joinColumns = @JoinColumn(name = "meal_id"),
inverseJoinColumns = @JoinColumn(name = "ingredient_id"))
private List<IngredientWeight> ingredients;
@Column(length = 1000)
private String description;
private String imageUrl;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "meal_category", joinColumns = @JoinColumn(name = "meal_id"),
inverseJoinColumns = @JoinColumn(name = "category_id"))
private Set<Category> category;
@OneToMany(mappedBy = "meal", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MealInfo> mealInfo;
private Integer calories;
public Meal(MealForm mealForm) {
this.name = mealForm.getName();
this.description = mealForm.getDescription();
this.imageUrl = mealForm.getImageUrl();
this.category = mealForm.getCategory();
}
}
public class IngredientWeight {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(name = "ingredient_weight_id")
private Ingredient ingredient;
private Long weight;
@ManyToMany
@JoinTable(name = "meal_ingredient", joinColumns = @JoinColumn(name = "ingredient_id"),
inverseJoinColumns = @JoinColumn(name = "meal_id"))
private Set<Meal> meals;
}
public class Ingredient {
@Id
@GeneratedValue
private Long id;
private String name;
@Column(name = "calories")
private Float caloriesPer100g;
@Column(name = "proteins")
private Float proteinPer100g;
@Column(name = "carbohydrates")
private Float carbohydratePer100g;
@Column(name = "fat")
private Float fatPer100g;
@OneToMany(mappedBy = "ingredient", cascade = {CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE},
fetch = FetchType.EAGER)
private List<IngredientWeight> ingredientWeights;
}
您能写出如何测试此方法或测试实现的吗?或者,也许您有任何公共存储库可以测试像这样的更大方法?
答案 0 :(得分:0)
如前所述,您可能不希望在服务中使用字段user
,shoppingList
和meals
。这些字段使服务在Web应用程序或Web服务(可以同时由多个客户端访问,因此可以同时访问多个线程)等多线程环境中使用时不安全。例如,如果另一个线程进入shoppingList
,则正在处理的createShoppingList
可能会在过程中途清除。相反,现在将这些字段设置为createShoppingList
方法内的局部变量。如果逻辑变得过于复杂而您的服务太大,则可以将其提取到单独的服务或帮助程序类中,该类在方法调用的开始处实例化,并在方法结束时丢弃。
我总是将单元测试编写为单个类的白盒测试。如果可以的话,我会尝试覆盖代码中的每个分支。您可以通过在IntelliJ中进行覆盖测试来运行此检查。请注意,黑盒测试也非常有用,它们专注于组件的“合同”。在我看来,单元测试通常不适合此操作,因为单个类的合同通常对于组件的整体功能不是很有趣,并且如果重构代码,则很容易更改。我将集成(或端到端)测试编写为黑盒测试。这就需要建立一个存根应用程序环境,例如,一个内存数据库以及可能通过WireMock提供的一些外部服务。如果您对此感兴趣,请查看Google的合同测试或RestAssured框架。
关于您的代码的一些评论:
public Map<Ingredient,Long> createShoppingList() {
// if any of the chained methods below return null, a NullPointerException occurs
// You could extract a method which takes the userInfoService user as an argument, see `findUser` below.
user = userRepository.findByLoginAndPassword(userInfoService.getUser().getLogin(),userInfoService.getUser().getPassword()).get();
// the above would then become:
User user = findUser(userInfoService.getUser()).orElseThrow(new ShoppingServiceException("User not found");
// instead of clearing these field, just initialize them as local variables:
shoppingList.clear();
meals.clear();
meals = user.getDiet().getMeals();
// I would change adjustIngredients so it doesn't return the meals but void
// it's expected that such a method modifies the meals without making a copy
meals = dietMealsService.adjustIngredients(meals);
// I would extract the below iteration into a separate method for clarity
for (MealInfo meal : meals) {
// I would also extract the processing of a single meal into a separate method
// the `meal.getIngredients` actually doesn't return Ingredients but IngredientWeights
// this is very confusing, I would rename the field to `ingredientWeights`
meal.getMeal().getIngredients().forEach(s -> {
// I would replace the four calls to s.getIngredient() with one call and a local variable
// and probably extract another method here
// You are using Ingredient as the key of a Map so you must implement
// `equals` and // `hashCode`. Otherwise you will be in for nasty
// surprises later when Java doesn't see your identical ingredients as
// equal. The simplest would be to use the database ID to determine equality.
if(shoppingList.containsKey(s.getIngredient()))
shoppingList.put(s.getIngredient(), s.getWeight()+shoppingList.get(s.getIngredient()));
else
shoppingList.put(s.getIngredient(),s.getWeight());
});
}
return shoppingList;
}
private Optional<User> findUser(my.service.User user) {
if (user != null) {
return userRepository.findByLoginAndPassword(user.getLogin(), user.getPassword());
}
else {
return Optional.empty();
}
}
private void processMeals(List<MealInfo> meals, Map<Ingredient, Long> shoppingList) {
for (MealInfo mealInfo : meals) {
processIngredientWeights(mealInfo.getMeal().getIngredients(), shoppingList);
}
}
private void processIngredientWeights(List<IngredientWeight> ingredientWeights, Map<Ingredient, Long> shoppingList) {
for (IngredientWeight ingredientWeight: ingredientWeights) {
processIngredientWeight(ingredientWeight, shoppingList);
}
}
private void processIngredientWeight(IngredientWeight ingredientWeight, Map<Ingredient, Long> shoppingList) {
Ingredient ingredient = ingredientWeight.getIngredient();
Long weight = shoppingList.getOrDefault(ingredient, 0L);
weight += ingredientWeight.getWeight();
shoppingList.put(ingredient, weight);
}
编辑:我再次查看了您的代码和域并进行了一些更改,请在此处查看示例代码:https://github.com/akoster/x-converter/blob/master/src/main/java/xcon/stackoverflow/shopping
由于'Info'类,域模型有些混乱。我将它们重命名如下:
MealInfo -> Meal
Meal -> Recipe (with a list of Ingredients)
IngredientInfo -> Ingredient (represents a certain amount of a FoodItem)
Ingredient -> FoodItem (e.g. 'broccoli')
我意识到该服务没有争议!有点奇怪。分开获取用户(例如,取决于当前登录/选定的用户)并将其传递到服务中是很有意义的,如上所述。 ShoppingListService现在看起来像这样:
public class ShoppingListService {
private DietMealsService dietMealsService;
public ShoppingListService(DietMealsService dietMealsService) {
this.dietMealsService = dietMealsService;
}
public ShoppingList createShoppingList(User user) {
List<Meal> meals = getMeals(user);
dietMealsService.adjustIngredients(meals);
return createShoppingList(meals);
}
private List<Meal> getMeals(User user) {
Diet diet = user.getDiet();
if (diet == null || diet.getMeals() == null || diet.getMeals().isEmpty()) {
throw new ShoppingServiceException("User doesn't have diet");
}
return diet.getMeals();
}
private ShoppingList createShoppingList(List<Meal> meals) {
ShoppingList shoppingList = new ShoppingList();
for (Meal meal : meals) {
processIngredientWeights(meal.getRecipe().getIngredients(), shoppingList);
}
return shoppingList;
}
private void processIngredientWeights(List<Ingredient> ingredients, ShoppingList shoppingList) {
for (Ingredient ingredient : ingredients) {
shoppingList.addWeight(ingredient);
}
}
}
我还引入了一个'ShoppingList'类,因为传递Map是一种代码味道,现在我可以将向购物清单中添加成分的逻辑移到该类中。
import lombok.Data;
@Data
public class ShoppingList {
private final Map<FoodItem, Long> ingredientWeights = new HashMap<>();
public void addWeight(Ingredient ingredient) {
FoodItem foodItem = ingredient.getFoodItem();
Long weight = ingredientWeights.getOrDefault(foodItem, 0L);
weight += ingredient.getWeight();
ingredientWeights.put(foodItem, weight);
}
}
此服务的单元测试现在看起来像这样:
@RunWith(MockitoJUnitRunner.class)
public class ShoppingListServiceTest {
@InjectMocks
private ShoppingListService instanceUnderTest;
@Mock
private DietMealsService dietMealsService;
@Mock
private User user;
@Mock
private Diet diet;
@Mock
private Meal meal;
@Test(expected = ShoppingServiceException.class)
public void testCreateShoppingListUserDietNull() {
// SETUP
User user = mock(User.class);
when(user.getDiet()).thenReturn(null);
// CALL
instanceUnderTest.createShoppingList(user);
}
@Test(expected = ShoppingServiceException.class)
public void testCreateShoppingListUserDietMealsNull() {
// SETUP
when(user.getDiet()).thenReturn(diet);
when(diet.getMeals()).thenReturn(null);
// CALL
instanceUnderTest.createShoppingList(user);
}
@Test(expected = ShoppingServiceException.class)
public void testCreateShoppingListUserDietMealsEmpty() {
// SETUP
when(user.getDiet()).thenReturn(diet);
List<Meal> meals = new ArrayList<>();
when(diet.getMeals()).thenReturn(meals);
// CALL
instanceUnderTest.createShoppingList(user);
}
@Test
public void testCreateShoppingListAdjustsIngredients() {
// SETUP
when(user.getDiet()).thenReturn(diet);
List<Meal> meals = Collections.singletonList(meal);
when(diet.getMeals()).thenReturn(meals);
// CALL
instanceUnderTest.createShoppingList(user);
// VERIFY
verify(dietMealsService).adjustIngredients(meals);
}
@Test
public void testCreateShoppingListAddsWeights() {
// SETUP
when(user.getDiet()).thenReturn(diet);
when(diet.getMeals()).thenReturn(Collections.singletonList(meal));
Recipe recipe = mock(Recipe.class);
when(meal.getRecipe()).thenReturn(recipe);
Ingredient ingredient1 = mock(Ingredient.class);
Ingredient ingredient2 = mock(Ingredient.class);
when(recipe.getIngredients()).thenReturn(Arrays.asList(ingredient1, ingredient2));
FoodItem foodItem = mock(FoodItem.class);
when(ingredient1.getFoodItem()).thenReturn(foodItem);
when(ingredient2.getFoodItem()).thenReturn(foodItem);
Long weight1 = 42L;
Long weight2 = 1337L;
when(ingredient1.getWeight()).thenReturn(weight1);
when(ingredient2.getWeight()).thenReturn(weight2);
// CALL
ShoppingList shoppingList = instanceUnderTest.createShoppingList(user);
// VERIFY
Long expectedWeight = weight1 + weight2;
Long actualWeight = shoppingList.getIngredientWeights().get(foodItem);
assertEquals(expectedWeight, actualWeight);
}
}
我希望这是不言自明的。
顺便说一句,请记住,单元测试只能测试被测类。尝试最小化关于其他类的行为的任何假设,并通过模拟它们使它们明确,如上所示。出于同样的原因,我总是尝试避免在单元测试中使用“真实的”测试数据,因为它表明值对测试很重要-无关紧要。
答案 1 :(得分:0)
我像Adrian所说的那样固定了设计,并为此方法创建了一个测试。关于下面的代码,我有几个问题:
您如何看待我的考试? setUp
方法中的第一部分及以上部分是必要的,或者我可以用更好的方式替换它吗?也许我只能在数据库中为测试创建示例实体吗?
我还应该测试哪些案例?
是否可以提取user.getDiet()
来分离方法checkDiet()
并在内部使用try-catch?
为什么我从变量login
中删除password
和user
字段时却得到ShoppingServiceException(“ User not found”),但我在这里{{1 }}
我重构的ShoppingServiceClass:
when(userInfoService.getUser()).thenReturn(user);
when(userRepository.findByLoginAndPassword(anyString(),anyString())).thenReturn(Optional.of(user));
和ShoppingServiceTest类:
@Service
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Data
@NoArgsConstructor
public class ShoppingListService {
UserInfoService userInfoService;
DietMealsService dietMealsService;
UserRepository userRepository;
@Autowired
public ShoppingListService(UserInfoService userInfoService, DietMealsService dietMealsService,UserRepository userRepository) {
this.userInfoService = userInfoService;
this.dietMealsService = dietMealsService;
this.userRepository = userRepository;
}
public Map<Ingredient,Long> createShoppingList() throws ShoppingServiceException {
User user = findUser(userInfoService.getUser()).orElseThrow(() -> new ShoppingServiceException("User not found"));
List<MealInfo> meals = checkDiet(user).getMeals();
dietMealsService.adjustMealsIngredients(meals);
Map<Ingredient, Long> shoppingList = new HashMap<>();
processMeals(meals, shoppingList);
return shoppingList;
}
private Optional<User> findUser(User user) {
if (user != null) {
return userRepository.findByLoginAndPassword(user.getLogin(), user.getPassword());
}
else {
return Optional.empty();
}
}
private Diet checkDiet(User user){
try{
user.getDiet().getMeals();
} catch(NullPointerException e){
throw new ShoppingServiceException("User doesn't have diet");
}
return user.getDiet();
}
private void processMeals(List<MealInfo> meals, Map<Ingredient, Long> shoppingList) {
for (MealInfo mealInfo : meals) {
processIngredientWeights(mealInfo.getMeal().getIngredientWeights(), shoppingList);
}
}
private void processIngredientWeights(List<IngredientWeight> ingredientWeights, Map<Ingredient, Long> shoppingList) {
for (IngredientWeight ingredientWeight: ingredientWeights) {
processIngredientWeight(ingredientWeight, shoppingList);
}
}
private void processIngredientWeight(IngredientWeight ingredientWeight, Map<Ingredient, Long> shoppingList) {
Ingredient ingredient = ingredientWeight.getIngredient();
Long weight = shoppingList.getOrDefault(ingredient, 0L);
weight += ingredientWeight.getWeight();
shoppingList.put(ingredient, weight);
}
}