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
130
votes
5 answers

Comparison between Mockito vs JMockit - why is Mockito voted better than JMockit?

I'm investigating which mocking framework to use for my project and have narrowed it down to JMockit and Mockito. I notice that Mockito was voted "the best mock framework for Java" on Stackoverflow. In comparing features on JMockit's "Mocking Tool…
user63904
87
votes
7 answers

Counting method invocations in Unit tests

What is the best way to count method invocations in a Unit Test. Do any of the testing frameworks allow that?
user855
  • 19,048
  • 38
  • 98
  • 162
63
votes
4 answers

Mock private static final field using mockito or Jmockit

I am using private static final LOGGER field in my class and I want LOGGER.isInfoEnabled() method to return false. How can I mock the static final field by using mockito or jMockit My class is: import org.slf4j.Logger; import…
RaT
  • 1,134
  • 3
  • 12
  • 28
52
votes
10 answers

Mocking Static Blocks in Java

My motto for Java is "just because Java has static blocks, it doesn't mean that you should be using them." Jokes aside, there are a lot of tricks in Java that make testing a nightmare. Two of the most I hate are Anonymous Classes and Static Blocks.…
Cem Catikkas
  • 7,171
  • 4
  • 29
  • 33
33
votes
9 answers

JMockit - initialization problem

When I use the following test I get a WARNING: WARNING: JMockit was initialized on demand, which may cause certain tests to fail; please check the documentation for better ways to get it initialized. This is my test implemenation: package…
Filip
  • 433
  • 1
  • 5
  • 7
26
votes
3 answers

How to mock a static method from JMockit

I have a static method which will be invoking from test method in a class as bellow public class MyClass { private static boolean mockMethod( String input ) { boolean value; //do something to value return value; } …
Roshanck
  • 2,220
  • 8
  • 41
  • 56
25
votes
5 answers

com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded

I get AttachNotSupportedException while running jmockit tests on linux (ubuntu 64bit). Java version is 1.7.0_51. This JDK is from Oracle. Tests are run using ant(that probably is not relevant) See the stack trace. [junit] [junit]…
Jayan
  • 18,003
  • 15
  • 89
  • 143
24
votes
7 answers

jmockit: Native library for Attach API not available in this JRE error

I was trying to use jmockit to unit test my project and got the following error: java.lang.UnsatisfiedLinkError: no attach in java.library.path java.lang.IllegalStateException: Native library for Attach API not available in this JRE at…
Dao Lam
  • 2,837
  • 11
  • 38
  • 44
22
votes
2 answers

JMockit Expectation API : How to throw an exception upon method/constructor invocation

While using JMockit I want to throw an exception upon a constructor invocation like this: new Expectations(){ { new FirefoxDriver();//Want to throw IllegalStateException here but how? } };
Affan Hasan
  • 334
  • 1
  • 4
  • 16
19
votes
5 answers

Mock private method in the same class that is being tested

I have a Java class named, MyClass, that I want to test with JUnit. The public method, methodA, that I want to test calls a private method, methodB, in the same class to determine which conditional path to follow. My goal is to write JUnit tests…
Kingand
  • 627
  • 1
  • 5
  • 12
18
votes
6 answers

Invoking a private method via JMockit to test result

Am using JMockit 1.1 and all I want to do is invoke a private method and test the return value. However, I am having trouble understanding exactly how to do this from the JMockit De-Encapsulation example. The method I am trying to test is the…
Robert Mark Bram
  • 8,104
  • 8
  • 52
  • 73
18
votes
1 answer

Return different values each time from jMockit expectation

I have a unit test where I am mocking java.net.URI class. Further, I am creating a jMockit NonStrictExpectation where I am expecting invocation of URI.getPath() and returning a particular string. The code being tested invokes URI.getPath() twice,…
Nagendra U M
  • 601
  • 2
  • 6
  • 16
17
votes
7 answers

how to mock a URL connection

Hi I have a method that takes an URL as an input and determines if it is reachable. Heres the code for that: public static boolean isUrlAccessible(final String urlToValidate) throws WAGNetworkException { URL url = null; …
Nemin
  • 1,907
  • 6
  • 24
  • 37
16
votes
2 answers

Does JMockit have any drawbacks at all?

This comparison shows, that JMockit has several advantages over other frameworks. Are there also any advantages that one of the others (JMock, EasyMock, Mockito, Unitils, PowerMock + Mockito/EasyMock) has over JMockit?
Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
15
votes
3 answers

How to mock only one static method and test the other

@Mocked Provider provider; public static class Provider { public static List getStaticList() { return new ArrayList<>(); } public static List test() { return getStaticList(); } } @Test public void…
T3rm1
  • 2,299
  • 5
  • 34
  • 51
1
2 3
54 55