~/java/20-linkage-error-loader-constraint-violation-junit-test-case-development-issue.md

Linkage Error Loader Constraint Violation - JUnit test case development issue

1 min readJuly 22, 2017by Gorav Singal#junit#test-cases#java
// TL;DR

LinkageError occurs when mock libraries cannot mock certain classes in your dependency tree; the fix involves excluding conflicting dependencies or using PowerMock for unsupported classes.

Linkage Error Loader Constraint Violation - JUnit test case development issue

Its good to write unit tests cases, and this part is mostly forgotten by developers. It can ease various issues that can come up while integrations.

I’m listing down various issues or problems that we faced while writing JUnit test cases:

LinkageError: Loader constraint violation

Stacktrace:

Solution

This is the issue you face when somewhere in your dependency you are using a component whose mock is not supported by mock libraries. Or, mock libraries can not mock a class.

In this case, the solution is to tell PowerMock that please ignore particular package to mock. We are asking mock loader not to load particular package/classes.

And, you can do this by putting an annotation on top of class like below:

@PowerMockIgnore("javax.management.*")

And, next time you run, the issue is gone. Enjoy

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to mock a constructor - Junit test case development issues

How to mock a constructor - Junit test case development issues

While writing JUnit test cases, we encounter cases like we want to initialize a…

Java - Union and Intersection of two lists

Java - Union and Intersection of two lists

Suppose you have two lists, and you want Union and Intersection of those two…

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…

Fix: SAML Message Intended Destination Did Not Match Recipient

Fix: SAML Message Intended Destination Did Not Match Recipient

Introduction I was trying to integrate Okta with Spring, and when I deploy the…

Spring - Learn Multiple Ways to use PackageScan Annotation

Spring - Learn Multiple Ways to use PackageScan Annotation

Introduction In this post, we will see multiple ways to use annotation…

Spring Boot - Fixing Autowire Bean Not found

Spring Boot - Fixing Autowire Bean Not found

Introduction In a Spring boot app, we tend to use annotation, so that Spring…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…