yaw
yaw

Reputation: 309

Incorrect assertion in junit / mockito

There is testing method:

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

and there is test class:

@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));
    }

The problem is that assertion returns error:

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)

While printLine return respectively false and true, but it's incorrect. Where's the problem?

Also, I'v got problem with my Intellij Idea - it skipping set breakpoints inside mocking class and I don't know why.

Upvotes: 0

Views: 788

Answers (1)

Amongalen
Amongalen

Reputation: 3131

In your code otherBean is just a mock and you call isFoo() on a mock without telling it to return any specific value. The default value returned by a boolean returning method is false.

I'm not sure what your plans were but I don't think that is what you tried to do.

If you intended to test OtherBean then you need an actual object and inject mocks of the dependencies. It could look something like this:

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));
  }
}

Upvotes: 2

Related Questions