Questions tagged [jmockit]

JMockit is a java framework for mocking objects in JUnit testing. It uses the instrumentation apis that modify the bytecode during run time to dynamically create classes. It allows developers to write unit tests without the testability issues typically found with other mocking APIs. Tests can be written that will mock final classes, static methods, constructors, and so on. The API also provides advanced support for integration tests and code coverage tool.

JMockit is a Java mocking API with expressive recording & verification syntax in JUnit/TestNG testing.

It provides mocking by using instrumentation APIs that modify class bytecode during runtime, allowing developers to write unit tests without the testability issues typically found with other mocking APIs.

Tests can be written to mock final classes, static methods, constructors, and so on. The API also provides advanced support for out-of-container integration testing for Java EE and Spring-based apps and code coverage tool with line and path metrics.

816 questions
5
votes
1 answer

jmockit mocked constructor not returning expected value

I am trying to unit test a class where one of its methods returns an instance of a collaborator class: depending on the values of its arguments it either returns a newly created instance, or a saved, previously created instance. I mock the…
Jim Renkel
  • 53
  • 1
  • 4
5
votes
5 answers

How to JMockIt System.getenv(String)?

What I have right now I have a 3rd party singleton instance that my class under test relies on and that singleton is using System.getenv(String) in its constructor. Is it possible to mock this call? I tried this JMockIt Example new…
cringe
  • 13,401
  • 15
  • 69
  • 102
5
votes
5 answers

Check that a method is not invoked

I want to check that a method is not run and tried to do it with an Expectation setting times = 0;, however I don't get the expected behaviour. For example, the following test passes, although the Session#stop method is called, and the expectation…
assylias
  • 321,522
  • 82
  • 660
  • 783
4
votes
2 answers

jmockit, openJDK and UnsatisfiedLinkError

When trying to run test with JMockit on OpenJDK 6, I'm facing the error: [junit] Exception in thread "main" java.lang.ExceptionInInitializerError [junit] at…
jdevelop
  • 12,176
  • 10
  • 56
  • 112
4
votes
4 answers

ClassFormatError using JMockit with EMMA

I am trying to use EMMA to measure coverage of some JUnit tests that use JMockit. But when I try to run the JMockit tests after instrumenting with EMMA, about a quarter of the tests fail with the following…
Chris B
  • 9,149
  • 4
  • 32
  • 38
4
votes
3 answers

JMockit & multiple local methods

Let's say I have class MyClass with methods x(), y() and z(). Let's say x() calls y(), and y() calls z(). So everytime I test x() both y() and z() are called. In case of mocking the dependencies of MyClass I will have to mock the dependencies…
antonio.fornie
  • 1,740
  • 2
  • 13
  • 23
4
votes
1 answer

Java11 and JMockit 1.43 Java.lang.UnsatisfiedLinkError happened when Jmockit mockup System.currentTimeMillis

I am trying to mock System.currentTimeMillis using jmockit1.43 using below code private static class SystemMock extends MockUp{ @Mock public static long currentTimeMillis() { return ourMockCurrentTime; } } But I am…
learner
  • 1,952
  • 7
  • 33
  • 62
4
votes
4 answers

How do I mock a method with generic objects in JMockit?

This question is self explanatory if you know how to use JMockit: How do I mock a method that has generics on it? I want to mock this method: public T save(T entity) but it always throws an exception like…
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
4
votes
1 answer

jMockit Error on Initialization: java.lang.IllegalStateException: Running on JDK 9

jmockit cannot initialize in eclipse: java.lang.IllegalStateException: Running on JDK 9 requires -javaagent:/jmockit-1.n.jar or -Djdk.attach.allowAttachSelf If I add the -Djdk.attach.allowAttachSelf VM argument, it works fine. Why would I…
user2706641
  • 67
  • 1
  • 4
4
votes
4 answers

How to mock the default constructor of the Date class with JMockit?

I want to mock the default constructor of java.util.date so it does not construct a Date object representing the time when it was created, but always the same Date object (in my example below 31 Dec 2010). I tried doing this with JMockit and JUnit,…
asmaier
  • 11,132
  • 11
  • 76
  • 103
4
votes
1 answer

Exclude enum classes from code coverage?

Till now I'm excluding certain classes depending on their package names or class names. Is there any way I can exclude Enums like given below from coverage? package com.*; public enum Quality { high,normal,low; } For Coverage I'm using…
Kaleb
  • 616
  • 1
  • 9
  • 22
4
votes
0 answers

Avoiding unused warning for @Mocked fields in IDEA

When I have some fields in my Unit test which are @Mocked: @Mocked private MyService service; the IDEA (and I guess Java too) produces unused warning, because it's never being assigned value (since it's just being populated by JMockit) - despite…
Michal
  • 1,262
  • 1
  • 12
  • 22
4
votes
1 answer

JMockit mock not getting destroyed

Using JUnit for testing classes- Have a class TestAccess.java having static private TestAccess instance = new TestAccess(); public static TestAccess getTestAccess() { returns instance; } For testing a test class A.java used JMockit to mock the…
user5591691
4
votes
3 answers

How to mock/fake a new enum value with JMockit

How can I add a fake enum value using JMockit? I could't find anything in the documentation. Is it even possible? Related: this question but it is for mockito only, not for JMockIt. EDIT: I removed the examples I gave in the first place because the…
dyesdyes
  • 1,147
  • 3
  • 24
  • 39
4
votes
1 answer

JMockit: How to debug tests when using the @Tested annotation?

The questions Debug Partial Mock in JMockit and Debugging Java code tested with Spock and JMockit already deal with the issue, that breakpoints in your software under test (SUT) get ignored when the class gets redefined/instrumented by JMockit. The…
Joern
  • 1,926
  • 1
  • 13
  • 18