My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Showing posts with label unit test. Show all posts
Showing posts with label unit test. Show all posts

Monday, November 30, 2020

Mockito MockMaker Java IllegalStateException Error: How to Fix Mockito Plugin error in Testing

 There are times when you will land into problems while running Mock tests using Mockito. A Sample error could be like:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker


The issue is that the Mockito core depends on a library called byte-buddy and this problem is mostly occurred when mockito doesn’t find a matching byte buddy jar version.

Error screenshot:


To Solve this issue find out the mockito core version your project is using.

In eclipse, you can check in project build path by navigating as follows:

Right click on project -> Properties -> Java build path -> Libraries tab


For IntelliJ This could be different. Look the screenshot below:



Search maven repository for that version of mockito core. To me, it was : https://mvnrepository.com/artifact/org.mockito/mockito-core/2.7.1



Description of issue:

The actual issue it seems is because of error from ByteBuddyAgent. I found following stacktraces that pointed me to the error:

Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@e32787d

at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:316)

at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<clinit>(InlineByteBuddyMockMaker.java:102)

... 34 more

Caused by: java.lang.reflect.InvocationTargetException

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:303)


Solution to fix the error:

Look at the Compile Dependencies section. Note down the correct dependent version of byte-buddy and include in the project. If the jar is already included with some other version, override the version with this correct version.

Also, i did see following in stacktrace:

[ERROR] error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by /modules/java.base/java/lang/Object.class


So, fix above two issues. Build the application and run the tests again. Your issue related to MockMaker must be solved by now.

Voila :)

Enjoy!


Friday, January 31, 2020

Build using Maven - MVN and POM.xml

In order to build a Maven project you can use one of the following:

  • mvn clean install
  • mvn clean package etc.

This would build/compile and run the tests as well.

However, if you want to run the tests only then what would you do?

Sound similar? Yeah! we all often land up in this situation as an application developer. And, the solution is simple.


You can use mvn test to run unit test in Maven. Few examples :
# Run all the unit test classes.
$ mvn test

# Run a single test class.
$ mvn -Dtest=TestApp1 test

# Run multiple test classes.
$ mvn -Dtest=TestApp1,TestApp2 test

# Run a single test method from a test class.
$ mvn -Dtest=TestApp1#methodname test

# Run all test methods that match pattern 'testHello*' from a test class.
$ mvn -Dtest=TestApp1#testHello* test

# Run all test methods match pattern 'testHello*' and 'testMagic*' from a test class.
$ mvn -Dtest=TestApp1#testHello*+testMagic* test

The default maven-surefire-plugin is outdated, make sure update to the latest to support new features, like pattern matching or run a single test method, and etc.
pom.xml
 <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>

        </plugins>
    </build>