0

I use JUnit for unit-testing. I use JMockit to mock up some java.util classes in my unit tests:

   new MockUp<PrintWriter>() { //HERE UNIT TESTS HANG ON
        @SuppressWarnings("unused")
        @Mock(invocations = 5)
        public void print(String s) {
            System.out.print(s);
        }

        @SuppressWarnings("unused")
        @Mock(invocations = 1)
        public void flush() {}
    };

Problem: My unit test just hang on at mockup definition.

Question: May you suppose the problem?

My dependencies:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>1.7</version>
    </dependency>
</dependencies>
VB_
  • 45,112
  • 42
  • 145
  • 293
  • Same here with Mac Os x. It stucks in `MacosxVirtualMachine.read(int, byte[], int, int) line: not available [native method]` – perencia May 03 '14 at 09:34
  • @perencia so JMockit can't mock methods which has calls to native functions. Am I right? – VB_ May 03 '14 at 09:40
  • I'm not sure that is a problem.. – perencia May 03 '14 at 09:41
  • You should invert the order between the `junit` and `jmockit` dependencies, so that JMockit can properly install its integration with JUnit. There is no need for the `-javaagent` parameter, provided you are using a JDK 1.6+ with a working "Attach API" implementation (I recommend using the Oracle JDKs). – Rogério May 05 '14 at 15:01

2 Answers2

2

Please, have a look on this page:

http://jmockit.googlecode.com/svn-history/r1123/trunk/www/installation.html

at the step 4.

You are probably missing a jmockit agent as default VM argument:

-javaagent:<path_to>\jmockit.jar
Veronika
  • 143
  • 6
  • thanks. I use jdk 1.7. I'll try your solution a few hours later. – VB_ May 03 '14 at 09:48
  • My pleasure. I also use 1.7, but still had some trouble without the jmockit agent. I hope it will solve your problem. – Veronika May 03 '14 at 09:52
  • Note: the current version for that page is at http://jmockit.googlecode.com/svn/trunk/www/gettingStarted.html. – Rogério May 05 '14 at 14:57
1

The test class below works fine for me, using JMockit 1.7.

@RunWith(JMockit.class)
public class PrintWriterTest
{
    @Test
    public void useMockedPrintWriter() throws Exception {
        new MockUp<PrintWriter>() {
            @Mock(invocations = 5) void print(String s) { System.out.print(s); }
            @Mock(invocations = 1) void flush() {}
        };

        writeTextFileWithFiveLines();
    }

    void writeTextFileWithFiveLines() throws Exception {
        PrintWriter pw = new PrintWriter("temp.txt");
        pw.print("line1");
        pw.print("line2");
        pw.print("line3");
        pw.print("line4");
        pw.print("line5");
        pw.flush();
    }

    @Test
    public void useNonMockedPrintWriter() throws Exception {
        writeTextFileWithFiveLines();
    }
}

Note 1: The use of @RunWith(JMockit.class) above is not required; its advantage is only that it avoids the need to either have jmockit.jar precede junit.jar in the classpath or to have -javaagent:jmockit.jar as a JVM initialization parameter.

Note 2: The invocations = n constrains used above are entirely optional.

Rogério
  • 16,171
  • 2
  • 50
  • 63