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
6
votes
1 answer

Debugger won't work with JMockit

Hopefully an easy question here for someone..... I'm using RAD 7.5.2, and am writing Junit tests. I was writing them just fine with JUnit 3, and then I wanted to mock up some function calls. So I loaded up jmockit 0.9.7 and Junit 4.6. I also…
Jacob
6
votes
2 answers

How to mock out Thread.sleep() with JMockit?

I have the following code: class Sleeper { public void sleep(long duration) { try { Thread.sleep(duration); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } How do…
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
6
votes
3 answers

Maven surefire plugin problems with Jacoco, JMockit and JDK14

I tried to upgrade my project from JDK 11 to JDK 14, but running the tests failed after setting the java version to 14. As I am using jacoco in combination with JMockit I configured my build as follows (edit: JaCoCo version is 0.8.3 / 0.8.5, JMockit…
Frank Murmann
  • 71
  • 1
  • 2
  • 5
6
votes
2 answers

Setting javaagent in ant

I am trying to run JUnit tests from an Ant script. The tests use the JMockit mocking framework, which for Java 5 requires specifying it as a javaagent to run correctly. Here is the script I am running:
Michael K
  • 3,297
  • 3
  • 25
  • 37
6
votes
1 answer

Mock a static method multiple times using JMockit within a JUnit test

I have a class with static methods that I'm currently mocking with JMockit. Say it looks something like: public class Foo { public static FooValue getValue(Object something) { ... } public static enum FooValue { X, Y, Z, ...;…
pmc255
  • 1,499
  • 2
  • 19
  • 31
6
votes
0 answers

mocking a method call within PreAuthorize annotation

@Test public void getValueTest() throws Exception { Request request = new Request(); Response response = new Response(); } service class: @PreAuthorize("(hasRole(@role.one) or (hasRole(@role.two) or hasRole(@role.three))) and…
user3927150
  • 61
  • 1
  • 2
  • 9
6
votes
1 answer

Mocking and verifying SLF4J with JMockit

I have a class with SLF4J logger instanced like: public class MyClass { private static final Logger log = LoggerFactory.getLogger(MyClass.class); public void foo() { log.warn("My warn"); } } And I need to test it with JMockit…
mergoth
  • 71
  • 1
  • 6
6
votes
4 answers

Verify that overriden superclass method is called when invoking this method on subclass

I'll show my problem using this example: I have a class with a method foo. That class has a subclass which overrides this method. Subclass' method calls superclass' method. Can I verify that? I don't want to test what foo in superclass does. I just…
Pawel P.
  • 3,731
  • 4
  • 20
  • 20
6
votes
1 answer

What is the strategy to mock static loggers in java service using mockito

I see that loggers have been mocked with help of Powermock or some sort of overridden constructor which takes logger. Being logger used all over the code, is not their a simple way using just mockito? Some way to ignore the call or mock it - I do…
cpandey05
  • 1,241
  • 2
  • 17
  • 30
6
votes
1 answer

Test coverage for both Powermock and JMockit unit tests

Has anyone been able to get unit test coverage of both JMockit and Powermock unit tests working in JaCoCo from Maven build? I have an existing test set of Powermock unit tests, that I would like to gradually migrate to JMockit. But I need to be…
Justin Rowe
  • 2,356
  • 1
  • 20
  • 15
6
votes
2 answers

can jmockit and robolectric coexist?

I'm trying to implement a unit test using Robolectric to replace the stubbed methods in android.jar while also using jMockit to mock an Android class (Fragment, in my case). However, I can't seem to get it to work. If I annotate the test class…
Andy Dennie
  • 6,012
  • 2
  • 32
  • 51
6
votes
2 answers

JMockit multiple exceptions as result for method call

This is from the official JMockit Tutorial: @Test public void doSomethingHandlesSomeCheckedException() throws Exception { new Expectations() { DependencyAbc abc; { abc.stringReturningMethod(); …
Queequeg
  • 2,824
  • 8
  • 39
  • 66
6
votes
5 answers

How to mock JdbcTemplate.queryForObject() method

My method looks like this: public class Decompile extends JdbcDaoSupport public void getRunner(){ String val = this.getJdbcTemplate().queryForObject(sql,String.class, new Object[]{1001}); } } Please suggest how I would mock this.
buttowski
  • 4,657
  • 8
  • 27
  • 33
6
votes
2 answers

How to mock spring injected classes using JMockit

My code: class A extends X { @Autowired B b; @Override method() { //do something b.callMethodInB; //do something } } class B extends X { @Autowired C c; @Override method() { …
Ranjith
  • 163
  • 2
  • 11
5
votes
3 answers

Using jmockit expectations with matchers and primitive types

I'm using jmockit for unit testing (with TestNG), and I'm having trouble using the Expectations class to mock out a method that takes a primitive type (boolean) as a parameter, using a matcher. Here's some sample code that illustrates the…
Kris Pruden
  • 3,280
  • 4
  • 25
  • 30