0

I want to run my test, I was try many ways, with MockUp x = new MockUp<Hello> calling getMockInstance() but always I get this problem. I can never call the sayHello() with mark @Mark Some ideas?

<junit.version>4.12</junit.version>
<java.version>1.8</java.version>

<dependency>
    <groupId>org.jmockit</groupId>
    <artifactId>jmockit</artifactId>
    <version>1.26</version>
    <scope>test</scope>
</dependency>


<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>

My class:

public class Hello {
    
    public String sayHello() {
        return "hello world";
    }
}

My test:

@RunWith(JMockit.class)
public class HelloTest {


    @Tested
    public Hello mock;

    @Test
    public void myOwnTest() {
        new MockUp<Hello>() {
            @Mock
            public String sayHello() { // Method 'sayHello()' is never used 
                return "Mock hello world...!";
            }
        };

        String res = mock.sayHello();
    }
}

console:

Matching real methods not found for the following mocks:
Hector
  • 636
  • 8
  • 16

1 Answers1

0

First, update yourself to the latest version of JMockit (1.49) (or possibly the forked/improved 1.49.2). The minor versions fix and improve a great many things.

Secondly, the "@RunWith" is gone in modern versions in favor of javaagent.

It looks like you are doing a vanilla mocking exercise. Expectations are a lot easier to work with assuming you have access to it, which you do since you have a @Tested instance of it.

public class HelloTest {
    @Tested
    public Hello mock;
    @Test
    public void myOwnTest() {
        new Expectations {{
            mock.sayHello();
            result "Mock hello world...!";
        }};

        String res = mock.sayHello();
        assertEquals("Mock hello world...!", res);
    }
}

The MockUp approach would also work, but that's typically only used in more exotic situations (e.g. where you need to alter the behavior of some deep-down class very divorced from what you are testing). Typically, only for classes that can not be @Mocked (etc) in some other way.

Jeff Bennett
  • 996
  • 7
  • 18