繁体   English   中英

为什么此Mockito测试失败?

[英]Why is this Mockito test failing?

对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

这可能看起来很愚蠢,但确实无法解决我做错的事情。

你必须检查以错误的方式-如果你应该抛出一个异常quantity availableQuantity ,而不是更少。

即,您应该替换此检查:

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

有了这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM