Skip to content

Error Messages

Oli Bye edited this page Oct 31, 2018 · 1 revision

IllegalArgumentException: "can only set expectations on mock objects"

jmock-junit5 requires:

  1. @org.junit.jupiter.api.extension.RegisterExtension is used to register the JUnit5Mockery field
  2. The field type to be JUnit5Mockery. Otherwise JUnit5 will silently ignore the extension.
  3. The field type to be non-private. Otherwise, again, JUnit5 will silently ignore the extension.
package org.jmock.junit5.testdata;

import org.jmock.Expectations;
import org.jmock.auto.Mock;
import org.jmock.junit5.JUnit5Mockery;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class JUnit5TestThatDoesSatisfyExpectations {
    
    @RegisterExtension
    JUnit5Mockery context = new JUnit5Mockery();
    
    @Mock
    private Runnable runnable;
    
    @Test
    public void doesSatisfyExpectations() {
        context.checking(new Expectations() {{
            oneOf (runnable).run();
        }});
        
        runnable.run();
    }
}
Clone this wiki locally