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

How do I unit test a Java method which uses ProcessBuilder and Process?

I have a Java method which starts up a Process with ProcessBuilder, and pipes its output into a byte array, and then returns its byte array when the process is finished. Pseudo-code: ProcessBuilder b = new ProcessBuilder("my.exe") Process p =…
Epaga
  • 38,231
  • 58
  • 157
  • 245
14
votes
1 answer

Is Expectations redundant if I have Verifications in my test?

I'm confused as to the purpose of and difference between expectations and verifications. E.g. @Tested FooServiceImpl fooService; @Injectable FooDao fooDao; @Test public void callsFooDaoDelete() throws Exception { new Expectations() {{ …
fred
  • 1,812
  • 3
  • 37
  • 57
14
votes
3 answers

Mocking private method of class under test using JMockit

I want to mock private method of a class under test but method return false first two times when the method is called after that it should return false. Here is the code what I tried. This is the class which is being tested public class ClassToTest…
Varun
  • 209
  • 2
  • 5
  • 11
14
votes
3 answers

Mock a private static field with JMockit?

I have a class like the following; class ClassA { private static File myDir; // myDir is created at some stage private static String findFile(final String fileName) { for (final String actualBackupFileName : myDir.list()) { …
user2586917
  • 762
  • 1
  • 9
  • 26
13
votes
2 answers

Major difference between: Mockito and JMockIt

This is what I found from my initial attempts to use JMockIt. I must admit that I found the JMockIt documentation very terse for what it provides and hence I might have missed something. Nonetheless, this is what I understood: Mockito: List a =…
user855
  • 19,048
  • 38
  • 98
  • 162
13
votes
1 answer

JMockit mock constructor

I am unit testing a class which has a complicated constructor ( with lots of parameters ). Constructor takes three arguments like : public BehavioralDischargeCarePlan_Bus(Webform webForm,String dataEntryModel, String obsBatId) { …
userx
  • 1,083
  • 5
  • 18
  • 36
12
votes
5 answers

How to mock a method in an ENUM class?

I am working on writing JUNIT test case for my below ENUm class. My below class will only give me the hostname for the current machine where I am running my code. While I am writing JUNIT test, how can I mock the below class, so that I can change…
user2467545
11
votes
5 answers

Using JMockit to mock autowired interface implementations

We are writing JUnit tests for a class that uses Spring autowiring to inject a dependency which is some instance of an interface. Since the class under test never explicitly instantiates the dependency or has it passed in a constructor, it appears…
SwimsZoots
  • 387
  • 2
  • 4
  • 13
10
votes
1 answer

Can JMockit MockUp mock toString()?

Consider the following sample MockUp of a class Foo that intercepts Bar in the constructor and then implements toString() in terms of Bar; public class FooStub extends MockUp { private Bar bar; @Mock public void $init(Bar bar) { …
Bogdan Calmac
  • 7,993
  • 6
  • 51
  • 64
10
votes
3 answers

JMockIt fails with AgentInitializationException: Agent JAR loaded but agent failed to initialize

I am developing a plugin for Eclipse Kepler. Adding @RunWith(JMockit.class) to my test class causes the following error: JMockit: Reinitializing under custom class loader…
Sergey
  • 3,253
  • 2
  • 33
  • 55
9
votes
1 answer

What are the differences between @Mocked, @Injectable, and @Capturing?

First, I define a class, let's say Robot. public class Robot { private Vision vision; public Object recognizeObject(List> frames) { vision = new Vision(); return vision.recognize(frames); } } Class of…
Uvuvwevwevwe
  • 971
  • 14
  • 30
9
votes
4 answers

Does it make sense to Unittest wrapper methods

This question is somewhat philosophical. Given i have a Method like this: public List getStuffByName(@NotNull String name) throws SomeException { return someDependency.createQuery().byName(name).list().stream() …
billdoor
  • 1,999
  • 4
  • 28
  • 54
9
votes
0 answers

Robolectric test runs in Android Studio, but not by gradle in the console

I recently updated a few libraries in my gradle file, and as a result, some (but not all) of my unit tests fail when I run them in the console by "./gradlew clean test". However, all my tests pass successfully when I run them inside Android…
happyhuman
  • 1,541
  • 1
  • 16
  • 30
9
votes
1 answer

JMockit MockUp class and Mockito/mock equivalent

I am attempting to convert a test suite which uses JMockit to use Mockito & powermock. In the test setup there is the following snippet of code: new MockUp() { @Mock public boolean sendMessage(final String string1, final…
Mat
  • 515
  • 3
  • 8
  • 13
8
votes
1 answer

Equivalent of times() in JMockIt?

I dont think minInvocation or maxInvocation is equivalent to times() in Mockito. Is there? Please see this questions: Major difference between: Mockito and JMockIt which has not been answered yet by anyone. Edit I found the answer myself: Adding it…
user855
  • 19,048
  • 38
  • 98
  • 162
1
2
3
54 55