如何使用 Mockito 框架模拟我的服务
How to mock my service using Mockito framework
您好,我正在努力按照下面的方法编写模拟测试;
下面是我的代码;
@Component
public class CodeConfigBuilder {
@Value("${promoConfig.prefix.length}")
private Integer prefixLength;
public void validateRequestAndSetDefaults(PromoRequest promoRequest) {
prefixAndPostFixControlAccordingToLength(promoRequest);
}
private void prefixAndPostFixControlAccordingToLength(PromoRequest promoRequest) {
if (promoRequest.getPostfix() != null) {
int lengthControl = prefixLength + promoRequest.getPostfix().length();
if (lengthControl >= promoRequest.getLength()) {
throw new BadRequestException(Constant.ClientConstants.THE_SUM_OF_PREFIX_AND_POSTFIX_CAN_NOT_BE_GREATER_THAN_LENGHT);
}
}
}
}
下面是我的yml配置;
#========= Promo Config ========== #
promoConfig:
prefix:
length: 3
下面是我的服务;
public void validateRequest(PromoRequest promoRequest) {
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
我创建了一个 PropertySourceResolver class
@Component
@Getter
public class PropertySourceResolver {
private int prefixLength = 3;
下面是我的测试class;
@RunWith(MockitoJUnitRunner.class)
class CodeConfigBuilderTest {
@MockBean
private PropertySourceResolver propertySourceResolver = new PropertySourceResolver();
@InjectMocks
private PromoRequest promoRequest = new PromoRequest();
@InjectMocks
private PromoService Service;
@Before
public void init() {
promoRequest.setPrefix("ABC");
promoRequest.setPostfix("ABCDABCD");
promoRequest.setQuantity(10);
promoRequest.setLength(12);
promoRequest.setCharset("ABCDEF");
}
@Test
public void prefixAndPostFixControl_accordingToLength_for_Succeed() {
int lengthControl = promoRequest.getPrefix().length() + promoRequest.getPostfix().length();
if (lengthControl >= promoRequest.getLength()) {
Assert.assertTrue(false);
}
}
我喜欢根据测试场景更改我的代码;什么时候……然后 return 有什么想法吗?谢谢。
为了简化和更好地组织您的代码,通过创建以下 class 抽象出注入配置的复杂性:
@ConfigurationProperties(prefix = "promoConfig.prefix")
public class PromoPrefixConfiguration {
private Integer length;
public Integer getLength() {
return length;
}
}
然后,您应该将其设为 CodeConfigBuilder
的依赖项:
@Component
public class CodeConfigBuilder {
private Integer prefixLength;
public CodeConfigBuilder(PromoPrefixConfiguration promoPrefixConfiguration) {
this.prefixLength = promoPrefixConfiguration.getLength();
}
public void validateRequestAndSetDefaults(PromoRequest promoRequest) {
prefixAndPostFixControlAccordingToLength(promoRequest);
}
private void prefixAndPostFixControlAccordingToLength(PromoRequest promoRequest) {
if (promoRequest.getPostfix() != null) {
int lengthControl = prefixLength + promoRequest.getPostfix().length();
if (lengthControl >= promoRequest.getLength()) {
throw new BadRequestException(Constant.ClientConstants.THE_SUM_OF_PREFIX_AND_POSTFIX_CAN_NOT_BE_GREATER_THAN_LENGHT);
}
}
}
}
现在,为了测试这个 class 你必须进行三个测试:
- 测试
PromoRequest
是否有效,因为 postfix
为空;
- 测试
PromoRequest
是有效的,因为长度是有效的;
- 测试
PromoRequest
无效,因为长度无效;
它们将类似于以下内容:
class CodeConfigBuilderTest {
private PromoPrefixConfiguration promoPrefixConfiguration = new PromoPrefixConfiguration(10);
private CodeConfigBuilder codeConfigBuilder = new CodeConfigBuilder(promoPrefixConfiguration);
@Test
public void promoRequestIsValidGivenNullPostfix() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPostfix(null);
// Act
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
@Test
public void promoRequestIsValidGivenValidPrefixPlusPostfixLength() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPostfix("ABCD");
promoRequest.setLength(15);
// Act
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
@Test(expected = BadRequestException.class)
public void promoRequestIsInvalidGivenInvalidPrefixPlusPostfixLength() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPostfix("ABCDEFGH");
promoRequest.setLength(15);
// Act
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
}
如果您使用的是 JUnit 5,您可以将最后一个测试替换为以下测试以轻松断言异常消息:
@Test
public void promoRequestIsInvalidGivenInvalidPrefixPlusPostfixLength() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPrefix("ABCDEFG");
promoRequest.setPostfix("HIJKLMN");
// Act
Exception exception = assertThrows(BadRequestException.class, () -> {
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
});
// Assert
String exceptionMessage = exception.getMessage();
assertTrue(exceptionMessage.equals(Constant.ClientConstants.THE_SUM_OF_PREFIX_AND_POSTFIX_CAN_NOT_BE_GREATER_THAN_LENGHT));
}
P.S.: 你是说 suffix
而不是 postfix
?
您好,我正在努力按照下面的方法编写模拟测试;
下面是我的代码;
@Component
public class CodeConfigBuilder {
@Value("${promoConfig.prefix.length}")
private Integer prefixLength;
public void validateRequestAndSetDefaults(PromoRequest promoRequest) {
prefixAndPostFixControlAccordingToLength(promoRequest);
}
private void prefixAndPostFixControlAccordingToLength(PromoRequest promoRequest) {
if (promoRequest.getPostfix() != null) {
int lengthControl = prefixLength + promoRequest.getPostfix().length();
if (lengthControl >= promoRequest.getLength()) {
throw new BadRequestException(Constant.ClientConstants.THE_SUM_OF_PREFIX_AND_POSTFIX_CAN_NOT_BE_GREATER_THAN_LENGHT);
}
}
}
}
下面是我的yml配置;
#========= Promo Config ========== #
promoConfig:
prefix:
length: 3
下面是我的服务;
public void validateRequest(PromoRequest promoRequest) {
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
我创建了一个 PropertySourceResolver class
@Component
@Getter
public class PropertySourceResolver {
private int prefixLength = 3;
下面是我的测试class;
@RunWith(MockitoJUnitRunner.class)
class CodeConfigBuilderTest {
@MockBean
private PropertySourceResolver propertySourceResolver = new PropertySourceResolver();
@InjectMocks
private PromoRequest promoRequest = new PromoRequest();
@InjectMocks
private PromoService Service;
@Before
public void init() {
promoRequest.setPrefix("ABC");
promoRequest.setPostfix("ABCDABCD");
promoRequest.setQuantity(10);
promoRequest.setLength(12);
promoRequest.setCharset("ABCDEF");
}
@Test
public void prefixAndPostFixControl_accordingToLength_for_Succeed() {
int lengthControl = promoRequest.getPrefix().length() + promoRequest.getPostfix().length();
if (lengthControl >= promoRequest.getLength()) {
Assert.assertTrue(false);
}
}
我喜欢根据测试场景更改我的代码;什么时候……然后 return 有什么想法吗?谢谢。
为了简化和更好地组织您的代码,通过创建以下 class 抽象出注入配置的复杂性:
@ConfigurationProperties(prefix = "promoConfig.prefix")
public class PromoPrefixConfiguration {
private Integer length;
public Integer getLength() {
return length;
}
}
然后,您应该将其设为 CodeConfigBuilder
的依赖项:
@Component
public class CodeConfigBuilder {
private Integer prefixLength;
public CodeConfigBuilder(PromoPrefixConfiguration promoPrefixConfiguration) {
this.prefixLength = promoPrefixConfiguration.getLength();
}
public void validateRequestAndSetDefaults(PromoRequest promoRequest) {
prefixAndPostFixControlAccordingToLength(promoRequest);
}
private void prefixAndPostFixControlAccordingToLength(PromoRequest promoRequest) {
if (promoRequest.getPostfix() != null) {
int lengthControl = prefixLength + promoRequest.getPostfix().length();
if (lengthControl >= promoRequest.getLength()) {
throw new BadRequestException(Constant.ClientConstants.THE_SUM_OF_PREFIX_AND_POSTFIX_CAN_NOT_BE_GREATER_THAN_LENGHT);
}
}
}
}
现在,为了测试这个 class 你必须进行三个测试:
- 测试
PromoRequest
是否有效,因为postfix
为空; - 测试
PromoRequest
是有效的,因为长度是有效的; - 测试
PromoRequest
无效,因为长度无效;
它们将类似于以下内容:
class CodeConfigBuilderTest {
private PromoPrefixConfiguration promoPrefixConfiguration = new PromoPrefixConfiguration(10);
private CodeConfigBuilder codeConfigBuilder = new CodeConfigBuilder(promoPrefixConfiguration);
@Test
public void promoRequestIsValidGivenNullPostfix() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPostfix(null);
// Act
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
@Test
public void promoRequestIsValidGivenValidPrefixPlusPostfixLength() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPostfix("ABCD");
promoRequest.setLength(15);
// Act
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
@Test(expected = BadRequestException.class)
public void promoRequestIsInvalidGivenInvalidPrefixPlusPostfixLength() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPostfix("ABCDEFGH");
promoRequest.setLength(15);
// Act
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
}
}
如果您使用的是 JUnit 5,您可以将最后一个测试替换为以下测试以轻松断言异常消息:
@Test
public void promoRequestIsInvalidGivenInvalidPrefixPlusPostfixLength() {
// Arrange
PromoRequest promoRequest = new PromoRequest();
promoRequest.setPrefix("ABCDEFG");
promoRequest.setPostfix("HIJKLMN");
// Act
Exception exception = assertThrows(BadRequestException.class, () -> {
codeConfigBuilder.validateRequestAndSetDefaults(promoRequest);
});
// Assert
String exceptionMessage = exception.getMessage();
assertTrue(exceptionMessage.equals(Constant.ClientConstants.THE_SUM_OF_PREFIX_AND_POSTFIX_CAN_NOT_BE_GREATER_THAN_LENGHT));
}
P.S.: 你是说 suffix
而不是 postfix
?