java - 为什么这个 Mockito 测试失败了?

标签 java unit-testing testing junit mockito

对 Mockito 完全陌生,这是我开始的内容:

被测类 User.java:

package com.test.mockito;

public class User {
    private ProductManager productManager;
    public boolean buy(Product product, int quantity) throws InsufficientProductsException {
        boolean transactionStatus=false;
        int availableQuantity = productManager.getAvailableProducts(product);
        if (quantity < availableQuantity) {
            throw new InsufficientProductsException();
        }
        productManager.orderProduct(product, quantity);
        transactionStatus=true;
        return transactionStatus;
    }
    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }
}

模拟对象:Product.java

package com.test.mockito;

public class Product {
}

ProductManager.java

package com.test.mockito;

public interface ProductManager {
    int getAvailableProducts(Product product);
    int orderProduct(Product product, int num);
}

异常类:InsufficientProductsException.java

package com.test.mockito;

public class InsufficientProductsException extends Exception {
    private static final long serialVersionUID = 1L;
}

最后是测试代码。

package com.test.mockito;

import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.*;

public class UserTest {
    private User user;
    private ProductManager productManager;
    private Product product;
    private int purchaseQuantity = 15;


    @Before
    public void setupMock() {
        user = new User();
        productManager = mock(ProductManager.class);
        user.setProductManager(productManager);
        product = mock(Product.class);

    }

    @Test(expected=InsufficientProductsException.class)
    public void purchaseButInsufficientAvailableQuantity() throws InsufficientProductsException {
        int availableQuantity = 3;
        System.out.println("Train getAvailableProducts(product) to return " + availableQuantity);
        when(productManager.getAvailableProducts(product)).thenReturn(availableQuantity);
        try {
            System.out.println("User.buy(" + purchaseQuantity + ") should fail with InsufficientProductsException");
            user.buy(product,purchaseQuantity);
        } catch (InsufficientProductsException e) {
            System.out.println("InsufficientProductsException is thrown");
            verify(productManager, times(0)).orderProduct(product, purchaseQuantity);
            System.out.println("Verified orderProduct(product, " + purchaseQuantity + ") is not called");
            throw e;
        }
    }

}

测试失败并出现,因为用户没有抛出预期的 InsufficientProductsException。 Maven 测试报告:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.test.mockito.UserTest
Train getAvailableProducts(product) to return 3
User.buy(15) should fail with InsufficientProductsException
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 3.66 sec <<< FAILURE!

Results :

Failed tests:   purchaseButInsufficientAvailableQuantity(com.test.mockito.UserTest): Expected exception: com.test.mockito.InsufficientProductsException

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

这可能看起来很傻,但我确实做错了。

最佳答案

您的检查方式有误 - 如果 quantityavailableQuantity 更多,您应该抛出异常,而不是更少。

即,您应该替换此检查:

if (quantity < availableQuantity) {
    throw new InsufficientProductsException();
}

有了这个:

if (quantity > availableQuantity) {
    throw new InsufficientProductsException();
}

关于java - 为什么这个 Mockito 测试失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892647/

相关文章:

java - 输入流未被读取

java - 在错误消息对话框之后显示输入对话框

javascript - 传递给组件的 ReactJS/Jest : How to test/mock a function,

angularjs - Angular 单元测试 : How do i call a named function

java - Robolectric、AsyncTasks 和线程

java - 应用服务器架构: access clustered server application through single address

java - JSF2.2 @Named 不起作用

java - Spock交互测试优先于异常

在 cmocka 中创建夹具

java - 设置测试自动化框架 Selendroid 的问题