gpt4 book ai didi

java - junit/mockito 中的断言不正确

转载 作者:行者123 更新时间:2023-12-02 00:59:47 26 4
gpt4 key购买 nike

有测试方法:

public boolean isFoo(SomeObj someObj) {
if(someObj != null) {
return bean.filter(someObj.getValue()) || (!bean.filter(someObj.getValue()) && !someObj.isParameter());
} else {
return true;
}
}

并且有测试类:

@RunWith(MockitoJUnitRunner.class)
public class OtherBeanTest{

@Mock
private OtherBean otherBean ;

@Mock
private Bean bean;

@Test
public void test() {
SomeObj someObj = new SomeObj().value("value").parameter(false);

when(bean.filter(anyString())).thenReturn(false);

System.out.println(otherBean.isFoo(someObj));
System.out.println(bean.filter(someObj.getValue()) || (!bean.filter(someObj.getValue()) && !someObj.isParameter()));

Assert.assertTrue(otherBean.isFoo(someObj));
}

问题是断言返回错误:

java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertTrue(Assert.java:52)
at OtherBeanTest.java)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

虽然 printLine 分别返回 false 和 true,但这是不正确的。问题出在哪里?

另外,我的 Intellij Idea 有问题 - 它跳过模拟类中设置的断点,我不知道为什么。

最佳答案

在您的代码中,otherBean 只是一个模拟,您可以在模拟上调用 isFoo(),而不告诉它返回任何特定值。 boolean 返回方法返回的默认值为false

我不确定您的计划是什么,但我不认为您试图这样做。

如果您打算测试 OtherBean,那么您需要一个实际的对象并注入(inject)依赖项的模拟。它可能看起来像这样:

public class OtherBeanTest{

@Mock
private Bean bean;

@Test
public void test() {
SomeObj someObj = new SomeObj().value("value").parameter(false);

when(bean.filter(anyString())).thenReturn(false);
OtherBean otherBean = new OtherBean();
otherBean.setBean(bean);

Assert.assertTrue(otherBean.isFoo(someObj));
}
}

关于java - junit/mockito 中的断言不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60811675/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com