当方法抛出异常甚至尝试捕获时,JUnit测试失败

时间:2018-11-26 10:01:47

标签: android junit4

我应该怎么做才能验证以下情况?

注意

我尝试了@Test(expected = NumberFormatException.class),但没有希望。另外,似乎有些人删除了try / catch块,并改为在方法签名处进行标记(如果知道,为什么不处理它)

经过测试的代码

public class CalculatorViewModel extends BaseObservable {

    Calculator calculator;
    String inputCheckAmount;
    String inputTipPercentage;
    TipCalculator tipCalculation;

    public CalculatorViewModel(Calculator calculator) {
        this.calculator = calculator;
        this.tipCalculation = new TipCalculator("", 0, 0,0, 0);

        if (calculator == null) {
            this.calculator = new Calculator();
        }
    }

    public void calculateTip() {

        try {
            double checkAmount = Double.parseDouble(inputCheckAmount);
            int tipPercentage = Integer.parseInt(inputTipPercentage);
            tipCalculation = calculator.calculatorTip(checkAmount, tipPercentage);
        } catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
    }
}

在测试课程中。

public class CalculatorViewModelTest {

    CalculatorViewModel viewModel;

    @Mock
    Calculator mockCalculator;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        viewModel = new CalculatorViewModel(mockCalculator);
    }

    @Test
    public void testCalculateTipBadTipPercentage() {
        // Arrange
        viewModel.inputCheckAmount = "10.00";
        viewModel.inputTipPercentage = "";

        // Act
        viewModel.calculateTip();

        // Asset
        verify(mockCalculator, never()).calculatorTip(anyDouble(), anyInt());
    }
}

错误日志

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.viinsoft.playground.mvvmmytipcalculator.viewmodel.CalculatorViewModel.calculateTip(CalculatorViewModel.java:26)
    at com.viinsoft.playground.mvvmmytipcalculator.viewmodel.CalculatorViewModelTest.testCalculateTipBadTipPercentage(CalculatorViewModelTest.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

java.lang.AssertionError: Expected exception: java.lang.NumberFormatException

0 个答案:

没有答案