8

I'm attempting (still) to learn the ins and outs of JMockit. Here's yet another example of a JMockit oddity I just don't get. Running the test with NonStrictExpectations works just fine. However, running with MockUp does not. I'm not sure why. Any ideas? I'm running JMockit 1.5.

Method to test:

private List<Foo> getFooList(List<FooStatement> fooStatements){
    List<Foo> FooList = new ArrayList<Foo>();

    for(FooStatement at: fooStatements){
        List<Foo> aList = at.getFoos();
        FooList.addAll(aList);
    }

    return FooList;
}

Successful Expectations Test

@Test
public void getFooListWithExpectationsTest(
        @Mocked final FooStatement mockFooStatement,
        @Mocked final Foo mockFoo
){

    List<FooStatement> fooStatementList = new ArrayList<>(Arrays.asList(
            mockFooStatement,
            mockFooStatement
    ));

    new NonStrictExpectations(){{
        mockFooStatement.getFoos();
        result = new ArrayList<Foo>(Arrays.asList(mockFoo));
    }};

    List<Foo> fooList = Deencapsulation.invoke(handler, "getFooList", fooStatementList);
    Assert.assertTrue(fooList.size() == 2);
}

Assertions Error (0 != 2) with MockUp

@Test
public void getFooListWithMockUpTest(
        @Mocked final FooStatement mockFooStatement,
        @Mocked final Foo mockFoo
){

    new MockUp<FooStatement>(){
        @Mock
        public List<Foo> getFoos(){
            return new ArrayList<Foo>(Arrays.asList(mockFoo));
        }
    };

    List<FooStatement> fooStatementList = new ArrayList<>(Arrays.asList(
            mockFooStatement,
            mockFooStatement
    ));

    List<Foo> fooList = Deencapsulation.invoke(handler, "getFooList", fooStatementList);
    Assert.assertTrue(fooList.size() == 2);
}
himanshuIIITian
  • 5,985
  • 6
  • 50
  • 70
Will Lovett
  • 1,241
  • 3
  • 18
  • 35

2 Answers2

4

You are using MockUp<?> incorrectly. MockUp<T? will tell JMockit to redefine a classes loaded to JVM so that instead of the real class initialization of FooStatement, it will replace them by the ones defined in the MockUp<FooStatement.

So basically MockUp<FooStatement> will automatically replace calls of new FooStatement().

Try something like:

@Test
public void getFooListWithMockUpTest(@Mocked final Foo mockFoo){

    new MockUp<FooStatement>(){
        @Mock
        public List<Foo> getFoos(){
            return new ArrayList<Foo>(Arrays.asList(mockFoo));
        }
    };

    List<FooStatement> fooStatementList = new ArrayList<>(Arrays.asList(
            new FooStatement(),
            new FooStatement()
    ));

    List<Foo> fooList = Deencapsulation.invoke(handler, "getFooList",     fooStatementList);
    Assert.assertTrue(fooList.size() == 2);
}
Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
  • Ah. That would make sense. FooStatement is an interface though so that means every time I instantiate one, I'd have to implement all of its methods. Doesn't seem new MockUp is the way to go in this instance then. Thank you for the help. – Will Lovett Feb 24 '15 at 17:49
  • Is there a way to use MockUp to only mock method calls? I guess that would be what Expectations would be for if not. – Will Lovett Feb 24 '15 at 17:51
  • 1
    You don't normally use both the Expectations API *and* the Mockups API in the same test. They are different APIs for different needs. "Expectations" is for behavior-oriented unit tests where you focus on method calls from the class under test to its collaborators, while "Mockups" is for fake implementations used in state-oriented tests. – Rogério Feb 24 '15 at 19:01
  • MockUp is deprecated. Please use Expectations. Please checkout official github page jmockit.github.io/changes.html – Vadiraj S J Jun 17 '19 at 10:55
-3

MockUp is deprecated, use Expectations

Vadiraj S J
  • 639
  • 10
  • 17
  • Can you provide your source? – Spik330 Jun 13 '19 at 23:52
  • Please check: https://jmockit.github.io/changes.html Version 1.39 (Mar 25, 2018): Removed the MockUp#getMockInstance() method, which was deprecated in version 1.37. Clearly official documentation says to NOT to use MockUp – Vadiraj S J Jun 14 '19 at 06:18
  • 4
    Version 1.37 (Nov 26, 2017): Deprecated the getMockInstance() method from the MockUp class. Normally, this method is only used to obtain an instance of a faked interface. For faked classes, it has no practical use. Instead of using a mock-up for an interface type, it's recommended to create a minimal implementation class, or to use an existing implementation. It doesn't mention anything about Not to use MockUp, it says to not use MockUp to make a mock for an interface. – Spik330 Jun 19 '19 at 22:30