【发布时间】:2021-04-17 10:00:00
【问题描述】:
我有一个 DistributionRule 类,其中包含私有 Set 分布;
@Getter
@Setter
public class DistributionRule extends BaseModel {
private String ruleName;
private String skuId;
private String catalogueId;
private String categoryId;
private Boolean active;
private RuleLevel level;
private Set<Distribution> distributions;
private Double thresholdValue;
private RuleType ruleType = RuleType.GENERAL;
@Getter
@Setter
public static class Distribution {
private String consumerId;
private Double distribution;
}
}
在RuleServiceImpl中有一个获取DistributionRule列表的方法:
@Override
public List<DistributionRule> getDistributionRules() {
return ruleRepository.findAll();
}
在 DivRuleApplicationTests 类中:
@RunWith(SpringRunner.class)
@SpringBootTest
class DivRuleApplicationTests {
@Autowired
private RuleServiceImpl ruleServiceImpl;
@MockBean
RuleRepository ruleRepository;
@Test
void test() {
assertTrue(true);
}
@Test
public void getDistributionRulesTest(){
Set<Distribution> distributions = new HashSet();
when(ruleRepository.findAll()).thenReturn(Stream
.of(new DistributionRule("rule0010", "0010", " ", " ", true, "SKU", ""),
new DistributionRule("rule0020", "0020", "", "", true, "SKU", ""))
.collect(Collectors.toList()));
assertEquals(2, ruleServiceImpl.getDistributionRules().size());
}
}
如何在 Stream.of() 中传递“分布”值?
【问题讨论】:
-
为什么不用setter传递呢? (假设你不能用构造函数传递它)
标签: junit static mocking mockito powermockito