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

Removing previously defined expectations in JMockit

I have an object that I'm mocking with a JMockit NonStrictExcpection() in the @Before/setUp() method of my test class so that it returns the value expected for normal execution of my class under test. This is fine for all of my test methods save for…
chrisbunney
  • 5,819
  • 7
  • 48
  • 67
8
votes
4 answers

Is there a way in JMockit to call the original method from a mocked method?

In my mock class, I'm mocking method foo(). For some test cases, I want the mock implementation of foo() to return a special value. For other test cases, I want to use the real implementation of foo(). I have a boolean defined in my mock class so…
Eric Asberry
  • 612
  • 1
  • 5
  • 11
8
votes
2 answers

JMockit - Expectations vs MockUp Why does one work and the other doesn't?

I'm attempting (still) to learn the ins and outs of JMockit. Here's yet another example of a JMockit oddity I just don't get. Running the test with NonStrictExpectations works just fine. However, running with MockUp does not. I'm not sure why. …
Will Lovett
  • 1,241
  • 3
  • 18
  • 35
8
votes
2 answers

Using kotlin with jmockit

I need some advice using jmockit with kotlin. (CUT) This is my (Java) class under test: public final class NutritionalConsultant { public static boolean isLunchTime() { int hour = LocalDateTime.now().getHour(); return hour >= 12…
sk_dev
  • 295
  • 2
  • 10
8
votes
1 answer

How to inject mocked dependencies with jmockit

Currently I try to understand how the @Injectable and @Tested annotations are working. I already did some tests and understood the concept but I didn't get how I can use those annotations in real world applications. Let's say we are developing a…
Philipp Waller
  • 463
  • 1
  • 7
  • 13
8
votes
4 answers

How to pass null string to a private method using jmockit while unit testing it?

Below is my private method in my DataTap class and I am trying to junit test this private method using jmockit - private void parseResponse(String response) throws Exception { if (response != null) { // some code } } So below is the…
john
  • 11,311
  • 40
  • 131
  • 251
7
votes
3 answers

JMockit with JUnit5 - JMockit didn't get initialized

When I am using the following Test method using JUnit 5 and JMockit, I am getting an error as: JMockit didn't get initialized; please check the -javaagent JVM initialization parameter was used These are my classes which I created and want to test: …
7
votes
2 answers

module java.base does not read module java.desktop

When I run this test (using jmockit and TestNG, not sure if that's relevant): public class Test { @Test public void test(@Mocked ProcessBuilder pb) throws IOException { new Expectations() {{ pb.start(); result = null; }}; assertNull(m()); …
assylias
  • 321,522
  • 82
  • 660
  • 783
7
votes
2 answers

Using JMockit and Spring AOP together

Suppose I have a program that looks like this: @Component public class MainAction { public void doTheAction() { System.out.println("Now doing the action"); } } @Aspect @Component public class BeforeAspect { @Autowired …
ajb
  • 31,309
  • 3
  • 58
  • 84
7
votes
2 answers

How to mock private getters?

I have a class that I want to test. It looks similar to this: public class ClassUnderTest { private Dependency1 dep1; private Dependency1 getDependency1() { if (dep1 == null) dep1 = new Dependency1(); return…
lg.lindstrom
  • 776
  • 1
  • 13
  • 31
7
votes
2 answers

Mocking non-public static methods in abstract classes with JMockit?

I have the following class: public abstract class AbstractParent { static String method() { return "OriginalOutput"; } } I want to mock this method. I decide to use JMockit. So I create a mock class: public class MockParent { …
Epaga
  • 38,231
  • 58
  • 157
  • 245
7
votes
1 answer

Can JMockit work with scala?

I googled little bit and it seems for me that it's possible. But when I tried like this: class Calc { def sum(first: Int, second: Int) = { first + second } } And "test" class: class CalcTest { @Test def testSum(@Mocked test: Calc) { …
Moses
  • 1,243
  • 3
  • 14
  • 21
7
votes
4 answers

java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath

I am getting below exception when i am trying to runing the Junit testcases. If i am changing the classpath entry order it is working fine but the Jococo coverage is not working.It is getting hanging. Can any please help to fix this…
user1166528
  • 381
  • 3
  • 7
  • 18
7
votes
3 answers

Verifying that a protected super method is invoked

I have the following structure: class Bar{ .... protected void restore(){ .... } .... } This class is extended by Foo as below: class Foo extends Bar{ .... @Override public void restore(){ //valid override …
arin
  • 1,774
  • 22
  • 35
7
votes
2 answers

MyClass stays mocked between two tests

I have two test classes, MyFirstTest and MySecondTest. Running each independently works fine. When I run both (in eclipse select the test folder which contains these files, right click, run as junit), MySecondTest fails because MyClass is still…
user1346730
  • 165
  • 4
  • 13
1 2
3
54 55