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
4
votes
2 answers

JMockit - warning when mocking methods that expect Collections

Is there a way to get the following mock to work without an Unchecked cast warning: new Expectations() {{ UrlService.addUrls((List)any); result = expectedCandidates; }}; The UrlService.addUrls() method's signature is: static…
KidCrippler
  • 1,633
  • 2
  • 19
  • 34
4
votes
2 answers

jmockit mockup, getting error java.lang.NoSuchFieldError: $MMB

I am trying to Mock an application using an Android Platform. Many class use super-classe which are part of the Android classes which are unaccessible to my Unit test, this is why I thought using JMockit would solve my problem. Note that I am using…
4
votes
0 answers

JMockit throws "No class with name java_lang_System$currentTimeMillis" when used from Groovy

A test that works in a .java file throws an exception when put into a .groovy file. Why is that? Groovy: 2.3.9 JMockit: 1.20 Java: 1.8.0_60 The code: @Test public void testSystemCurrentTimeMillis(@Mocked final System unused) { new…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
4
votes
4 answers

Verifying behavior when testing a method that throws an exception

Recently, in a code review, a colleague said "Tests should not have try-catch blocks". He also pointed me to information about ExpectedException, and I was able to use it to rewrite some of my tests that were performing checks on the exception…
ajb
  • 31,309
  • 3
  • 58
  • 84
4
votes
4 answers

Mockito/JMockit & Hamcrest matchers : How to verify Lists/Collections?

This 2013 post on SO asked how to use Hamcrest matchers to verify lists/collections invocations in Mockito. The accepted solution was to cast the Matcher to a (Collection). I'm trying to do something similar, but running into a class cast error. …
Eric B.
  • 23,425
  • 50
  • 169
  • 316
4
votes
1 answer

How to use Espresso + JMockit

I want to use Espresso and JMockito. But I don't run test. If you have resolve way, help me. I wrote some file(build.gradle(app, project), Test java) as follows. build.gradle(app) apply plugin: 'com.android.application' android { …
4
votes
1 answer

Wants to understand how @Tested works with JMockit

I am using JMockit since long.I would like to understand how @Tested works. Today i was trying to use it within my Test class. What i understand is Whatever class we wants to test we can mark it as @Tested. One thing which was confusing me about the…
4
votes
2 answers

Verify method in constructor was called

I have a constructor that calls a method, like this: public Foo(boolean runExtraStuff) { if (runExtraStuff){ doExtraStuff(); } } The doExtraStuff() method is running some additional commands that are not easily mocked themselves…
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
4
votes
3 answers

JMockit can't mock concrete collection from JRE

I have test cases that require to mock HashMap class but it appears that JMockit encounters difficulties when mocking it. The following code trigger a NPE in the Expectations block : public class TestKO { public class ClassUnderTest { private…
emgr
  • 53
  • 1
  • 5
4
votes
2 answers

JMockit MockUp persisting between Spock tests

I'm using a Spock test written in Groovy to test some Java code. I'm using JMockit to mock methods in the java code, as Spock only handles mocking Groovy classes. I'm running into a problem where a JMockit MockUp is persisting between tests. Such a…
mnd
  • 2,709
  • 3
  • 27
  • 48
4
votes
2 answers

How to match 'any' parameter type while mocking private method in Jmockit

I have a problem while using jmockit for the following scenario. Did a research on the web, but couldn't locate the answers yet. In the record phase, I am setting the expectation on an object that is partially mocked. While doing it, I would like…
Alex Vincent
  • 41
  • 1
  • 1
  • 5
4
votes
2 answers

Jmockit Expectations/Verifications for calls to private methods when testing a public method?

Can anyone advise if it is possible to use an expectations/verifications to test that private methods are being called the-right-number-of-times/right-parameters. The Class under test has been Mocked-Up - with one private method overridden. Am…
DMac
  • 41
  • 1
  • 1
  • 3
4
votes
1 answer

Include JMockit Coverage in Sonarqube dashboard

I'm looking for a way to include the JMockit-Coverage reports of my multi-module java project into our SonarQube managing this project. We already have the Cobertura reports in Sonar, but I'd like to have both available easily to be able to compare…
JR Utily
  • 1,792
  • 1
  • 23
  • 38
4
votes
1 answer

How to mock @PrePersist method?

How do I mock a @PrePersist method, e.g. preInit(), of an entity that I instantiate? I'm using TestNG. EasyMock is prefered. @Test(enabled = true) public void testCreateOrder() { // Instantiating the new mini order will automatically invoke the…
feder
  • 1,849
  • 2
  • 25
  • 43
4
votes
1 answer

How to send input to System.console.readLine(...) in Junit?

I have a main method I'd like to test. I'm just wondering how to pass what the console.readLine(...) and console.readLine(...) is expecting from my Junit test - without refactoring main(...) - I'm using JMockit if that would be of use here - i.e.…
user2586917
  • 762
  • 1
  • 9
  • 26