- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我开始学习mockito来测试我的类(class)。我知道如何用一个或两个模拟的小类来做到这一点,但是当我的服务更大时我遇到了问题。例如我有服务
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;
}
你能写一下如何测试这个方法或测试实现吗?或者也许您有任何公共(public)存储库可以测试像这样的更大的方法?
最佳答案
如上所述,您可能不希望服务中包含 user
、shoppingList
和 meals
字段。这些字段使得服务在多线程环境中使用不安全,例如 Web 应用程序或 Web 服务(可以由多个客户端同时访问,因此可以由多个线程访问)。例如,如果另一个线程进入 createShoppingList
,您正在处理的 shoppingList
可能会在流程中途被清除。相反,现在将这些字段设置为 createShoppingList 方法内的局部变量。如果逻辑变得太复杂并且您的服务太大,您可以将其提取到单独的服务或帮助程序类中,该类在方法调用开始时实例化并在方法调用结束时丢弃。
我总是将单元测试编写为单个类的白盒测试。如果可以的话,我会尝试覆盖代码中的每个分支。您可以通过在 IntelliJ 中运行覆盖率测试来检查这一点。请注意,黑盒测试也非常有用,它们专注于组件的“契约”。在我看来,单元测试通常不适合这种情况,因为单个类的契约通常对于组件的整体功能来说不是很有趣,并且如果重构代码,很容易更改。我将集成(或端到端)测试编写为黑盒测试。这需要设置一个 stub 应用程序环境,例如内存数据库,可能还需要通过 WireMock 提供一些外部服务。如果您对此感兴趣,请研究一下 Google 的契约(Contract)测试或 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 是一种代码味道,现在我可以将逻辑移动到该类中,以将购物 list 中的成分添加到该类中。
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);
}
}
我希望这是不言自明的。
顺便说一句,请记住,单元测试应该只测试被测类。尝试尽量减少对其他类行为的任何假设,并通过模拟它们来明确这一点,如上所示。出于同样的原因,我总是尝试避免在单元测试中使用“现实”测试数据,因为它表明这些值对测试很重要 - 但事实并非如此。
关于java - 使用mockito在Spring中测试更大的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53760516/
我一直面临一个奇怪的问题。基本上,当我正常运行 Mockito 测试时,即“作为 Junit 测试运行”时,它给了我以下错误。有人可以帮助我请问我的错误是什么? 收到的错误: java.lan
我正在使用 Mockito 以及 mockito-inline用于模拟静态方法。我正在尝试申请 doNothing或类似的行为,到静态 void 方法。以下解决方法有效,但我认为应该有一种更方便的方法
我正在尝试验证我正在测试的类是否调用了正确的依赖类的方法。所以我试图匹配方法参数,但我并不真正关心这个测试中的实际值,因为我不想让我的测试变得脆弱。 但是,我在设置它时遇到了麻烦,因为 Mockito
我正在使用 Mockito 编写单元测试,并且在模拟注入(inject)的类时遇到问题。问题是两个注入(inject)的类是相同的类型,仅通过它们的 @Qualifier 注释进行区分。如果我尝试简单
在我的断言中的以下简单练习中,我期望 1,但得到 0。为什么我会看到这种行为? public class MockitoTest { POJO mockedPojo; @Before
我正在创建一个通用模拟客户端来测试 HTTP 交互。为此,我希望能够以相同的方法进行多次响应。使用普通模拟,这不是问题: when(mock.execute(any(), any(), any()))
我需要全局模拟类方法。 我的意思是,我不能创建模拟对象和 stub 方法。我的 api 不将此对象作为参数,所以我不能在函数调用中传递它,但是这个类的对象是在这些函数中创建并在那里使用的。这就是为什么
我正在尝试使用 Mockito 2.18.3 框架模拟我们公司内部库中提供的 final 类,不幸的是我们无权更改库中的代码。但每当我运行时,我都会收到以下错误: java.lang.NoClassD
研究了mockito测试框架,学习了powermock,突然发现一个叫powermockito的框架,看不懂了。 谁能告诉我这三个测试工具的区别? 最佳答案 Mockito 是市场标准模拟框架,味道非
我想跳过检查验证调用中的参数之一。因此对于: def allowMockitoVerify=Mockito.verify(msg,atLeastOnce()).handle(1st param,,3r
为了模拟在被测方法内部构造的本地对象上的局部变量/方法调用,我们目前使用的是 PowerMockito 库。 我们正在尝试评估是否可以使用 mockito-inline(版本 3.7.7)来做同样的事
我在想, 如果在 @Before 方法中我正在初始化模拟对象,我不应该在 @After 中取消对它的引用吗?或者那会是多余的吗?为什么? 最佳答案 不需要,JUnit 会为每个测试方法创建一个新的测试
我想使用 Mockito 验证字符串参数是否满足两个条件: verify(mockClass).doSomething(Matchers.startsWith("prefix")); verify(m
如果我像这样创建一个模拟 when(servicesTestEnv.mockUserProfileAndPortfolioTransactionRepository.get(servicesTestE
使用 Mockito 我遇到了以下问题: Mockito.when(restOperationMock.exchange( Mockito.anyString(), M
我想知道描述中的事情是否可行以及如何去做。 我知道你可以调用原始方法然后像这样做答案: when(presenter, "myMethod").doAnswer() 但我想对它们进行不同的排序,首先执
我试图弄清楚org.mockito.AdditionalMatchers是如何工作的,但我失败了。为什么这个测试失败了? import static org.hamcrest.CoreMatchers
有人知道使用 Mockito 为 ATG 编写单元测试用例吗?我在凝视时遇到了以下讨论 - Automated unit tests for ATG development和 Using PowerM
我想知道描述中的事情是否可行以及如何去做。 我知道你可以调用原始方法然后像这样做答案: when(presenter, "myMethod").doAnswer() 但我想对它们进行不同的排序,首先执
我有以下接口(interface)CatalogVersionService,它公开了一些服务。我还有一个单元测试,它通过使用 Mockito 来模拟这个接口(interface),如下所示: Cat
我是一名优秀的程序员,十分优秀!