Recently, while writing one of the NHibernate queries, using the NHibernate LINQ provider we faced a very interesting problem.
The issue was the NHibernate LINQ provider was not adding appropriate parenthesis to make the operator precedence more explicit.
The query was simple enough, we wanted to get a list of records based on certain condition. Let's say, for the sake of this post, consider that there is an employees table with ID, Name and Salary columns. The DDL to create the employees tables on SQLServer looks like
NHibernate class against which this table is mapped is called Employee. The Employee class and its mapping file looks like
As you can see there is nothing fancy about the table or the mapping.
Let's talk a bit about the query we want to fire against the employees table. We want to get list of employees whose name equals "Git" OR whose salary is between 1000 and 10000.
The SQL that would do the job for us would look like:
Again nothing remotely fancy about the SQL.
Let's try and write the same query using, NHibernate LINQ provider and lets look at the generated SQL. The NHibernate LINQ query to get similar results would look like:
The where condition is the exact replica of the SQL where condition that we had seen earlier. Only difference this time around is, we are using the LINQ syntax.
So far so good, the problem starts when we look at the SQL that is generated. The simplified version of the SQL generated from the above LINQ query looks like
I have removed all the named parameters that NHibernate adds, but essentially the query looks like the one shown above.
Whats wrong with the query?
Notice that there are no parenthesis to enforce the operator precedence! Because of this, it might get confusing to identify the order in which the operators will be process.
Consider a case when you have a complex query with many conditions and if proper parenthisis are not applied then it could get confusing.
I tried various permution and combination to force the NHibernate LINQ provider to spitout the parenthesis correctly, however I was not successful in doing so.
Finally, I decided to switch to the QueryOver API provided by NHibernate. QueryOver API is a Fluent wrapper over the NHibernate Criteria API. Using the QueryOver API the above code would look like
As you can see the nothing much has changed. The only changes are, .Query() call has been changed to .QueryOver() and instead of .ToList() we have to call .List().
Let's look at the generated SQL Query:
As you can see, it correctly adds the parenthesis to make the operator precedence more explicit!
Showing posts with label fixing. Show all posts
Showing posts with label fixing. Show all posts
Sunday, November 6, 2011
Monday, September 5, 2011
Fixing the The virtual path '/test/currentcontext.dummy' maps to another application, which is not allowed
We have recently integrated Spring.NET in an MVC3 application. In one of my previous post, I have already documented the detailed process of integrating Spring.NET with MVC3 application. Spring.NET is really helpful when it comes to writing Modular, Testable code. I totally recommend Spring.NET as Dependency Injection framework of choice!
We did a fresh deployment of our application on a new server. On this computer, when we tried to access any of the pages of our application we got an error which looked like
That was not very encouraging. How can an application that was working correctly on all other servers stared giving exceptions for this particular server?
We tried to find out what were the differences between the other server and this new server. We narrowed it down to one very interesting difference, we used to deploy our project as a website in IIS on all other server. While on the server that was giving us exceptions, we deployed our application as a virtual directory under the default website. This very curious difference was giving us exceptions while initializing the spring context.
I Googled a little and found out that this is a known issue which has been reported here and here.
The workaround to this issue was simple enough, we just have to give a name the spring context in web.config.
Would any random name work? Do we have to follow some convention?
Our Spring.NET section in web.config file looked like
By now it should be clear that, we have to add a name attribute to node. But the question is, what value should it hold?
The answer is pretty simple, the Spring.NET context should be named after the virtual directory under which the application is deployed. For example, if the virtual directory under which the application is deployed is called springmvc. The updated web.config should look like
This did the trick! After making this change we were able to browse all pages in our application and Spring.NET integration with MVC3 application was working well!
Small issues like these have very simple solutions but, one might end up spending quite some time before finding a satisfactory solution. I decided to document my finding for the benefit of the community!
We did a fresh deployment of our application on a new server. On this computer, when we tried to access any of the pages of our application we got an error which looked like
![]() |
| Virtual Path Maps to Another Application, is not allowed |
We tried to find out what were the differences between the other server and this new server. We narrowed it down to one very interesting difference, we used to deploy our project as a website in IIS on all other server. While on the server that was giving us exceptions, we deployed our application as a virtual directory under the default website. This very curious difference was giving us exceptions while initializing the spring context.
I Googled a little and found out that this is a known issue which has been reported here and here.
The workaround to this issue was simple enough, we just have to give a name the spring context in web.config.
Would any random name work? Do we have to follow some convention?
Our Spring.NET section in web.config file looked like
By now it should be clear that, we have to add a name attribute to
This did the trick! After making this change we were able to browse all pages in our application and Spring.NET integration with MVC3 application was working well!
Small issues like these have very simple solutions but, one might end up spending quite some time before finding a satisfactory solution. I decided to document my finding for the benefit of the community!
Saturday, June 11, 2011
Visual Studio 2010 Fixing "The referenced project XX does not exist" Error
Recently one of my friend was facing a weird issue while building our application. The application was developed in C#. We were using MSBuild as the build tool.
What was the issue?
The issue was simple, she was not able to compile the application.
The issue was special in many ways because, all of us (i.e. her team members) were able to compile the project just fine. Even the continuous integration service - Jenkins (formally Hudson), was able to compile the application. We were not able to compile the application only on her machine. The build failed with the error
The project "AB.dosomeprojects.Services.FillImporter.Service.csproj" referred the project "abcdefgh.dosomeprojects.Common.csproj" which was in the path "..\..\..\abcdefgh.dosomeprojects\abcdefgh.dosomeprojects.Common". The path was relative to the Service project directory.
The directory structure was correct, Common project was correctly reffered from the Service project, but still we were not able to build the app on her machine.
Initially, we thought that there must me some local changes on her machine because of which, she was not able to run the build on her machine. Build was not passing neither with Visual Studio 2010 nor with MSBuild and that too only on her machine.
We looked for any local changes on her machine, but unfortunately her machine was clean. There were no local changes. We were simply clueless! Why does the project build fine on other developers machine while it does not build on her machine?
After some intense Googling, we found an interesting hidden treasure shipped silently by Microsoft Visual Studio 2010. I call such issues as hidden treasure because, I can hardly believe my ears when I hear/read about such an issue.
I am sure it will come as an surprise to you as well. The issue is:
The Real Problem:
Visual Studio 2010 fails to build the project when the following conditions are met:
Condition 1: We have solution with multiple projects where there exists dependency relationships among them
Yes, we do have the solution with multiple projects in it and one project depends on the other. In our case the AB.dosomeprojects.Services.FillImporter.Service.csproj depends on the abcdefgh.dosomeprojects.Common.csproj project. Hence, condition 1 is met.
Condition 2: The sum of the following two path length is exactly added up to 259 characters (= MAX_PATH – 1)
This one is tricky. Which path sum are we talking about?
Condition 2.1: The path of a referencing project’s directory.
The path of the referencing project's directory, in our case the path of the AB.dosomeprojects.Services.FillImporter.Service.csproj on the file system, which was, "E:\projects\ABC\SVN\dosomeprojectsweb\trunk\AB.dosomeprojects.Services\AB.dosomeprojects.Services.FillImporter\AB.dosomeprojects.Services.FillImporter.Service" has total length of 158 chars.
Condition 2.2: The relative path to a referenced project from the current directory (= a referencing project’s directory).
The relative path of the referenced project from the current directory, in our case its the relative path of abcdefgh.dosomeprojects.Common.csproj, which is, "..\..\..\abcdefgh.dosomeprojects\abcdefgh.dosomeprojects.Common\abcdefgh.dosomeprojects.Common.csproj" has total length of 101 chars.
The sum total of these two path lengths: 158 + 101 = 259!
Wow! condition 2 is also met.
As I said, we have uncovered an hidden treasure of Visual Studio 2010. Its unable to build the project when the path length of referencing project + path length of relative path to a referenced project = 259!
If the sum total is one less or one greater than 259 it all works fine! On other developers machine the total path length was less than or greater than 259 but was never equal to 259 and because of this reason we were all able to build the project but she was not able to build the project on her machine! Weird isn't?
This issue occurs due to a bug in the Path.GetFullPath in .NET Framework library. This is a known issue in Visual Studio 2010.
The Fix:
The workaround is pretty straight forward, rename one of the folders a bit longer or a bit shorter and make sure that, the sum of the two path length is NOT equal to 259 chars.
For more information on the issue visit the offical Microsoft Knowledge base article here http://support.microsoft.com/kb/2516078
Now you know, we are having so much fun with .Net (sarcasm intended)!
What was the issue?
The issue was simple, she was not able to compile the application.
The issue was special in many ways because, all of us (i.e. her team members) were able to compile the project just fine. Even the continuous integration service - Jenkins (formally Hudson), was able to compile the application. We were not able to compile the application only on her machine. The build failed with the error
The project "AB.dosomeprojects.Services.FillImporter.Service.csproj" referred the project "abcdefgh.dosomeprojects.Common.csproj" which was in the path "..\..\..\abcdefgh.dosomeprojects\abcdefgh.dosomeprojects.Common". The path was relative to the Service project directory.
The directory structure was correct, Common project was correctly reffered from the Service project, but still we were not able to build the app on her machine.
Initially, we thought that there must me some local changes on her machine because of which, she was not able to run the build on her machine. Build was not passing neither with Visual Studio 2010 nor with MSBuild and that too only on her machine.
We looked for any local changes on her machine, but unfortunately her machine was clean. There were no local changes. We were simply clueless! Why does the project build fine on other developers machine while it does not build on her machine?
After some intense Googling, we found an interesting hidden treasure shipped silently by Microsoft Visual Studio 2010. I call such issues as hidden treasure because, I can hardly believe my ears when I hear/read about such an issue.
I am sure it will come as an surprise to you as well. The issue is:
The Real Problem:
Visual Studio 2010 fails to build the project when the following conditions are met:
- Solution with multiple projects exists, where there exists dependency relationships among them
- The sum of the following two path length is exactly added up to 259 characters (= MAX_PATH – 1)
- The path of a referencing project’s directory.
- The relative path to a referenced project from the current directory (= a referencing project’s directory).
Condition 1: We have solution with multiple projects where there exists dependency relationships among them
Yes, we do have the solution with multiple projects in it and one project depends on the other. In our case the AB.dosomeprojects.Services.FillImporter.Service.csproj depends on the abcdefgh.dosomeprojects.Common.csproj project. Hence, condition 1 is met.
Condition 2: The sum of the following two path length is exactly added up to 259 characters (= MAX_PATH – 1)
This one is tricky. Which path sum are we talking about?
Condition 2.1: The path of a referencing project’s directory.
The path of the referencing project's directory, in our case the path of the AB.dosomeprojects.Services.FillImporter.Service.csproj on the file system, which was, "E:\projects\ABC\SVN\dosomeprojectsweb\trunk\AB.dosomeprojects.Services\AB.dosomeprojects.Services.FillImporter\AB.dosomeprojects.Services.FillImporter.Service" has total length of 158 chars.
Condition 2.2: The relative path to a referenced project from the current directory (= a referencing project’s directory).
The relative path of the referenced project from the current directory, in our case its the relative path of abcdefgh.dosomeprojects.Common.csproj, which is, "..\..\..\abcdefgh.dosomeprojects\abcdefgh.dosomeprojects.Common\abcdefgh.dosomeprojects.Common.csproj" has total length of 101 chars.
The sum total of these two path lengths: 158 + 101 = 259!
Wow! condition 2 is also met.
As I said, we have uncovered an hidden treasure of Visual Studio 2010. Its unable to build the project when the path length of referencing project + path length of relative path to a referenced project = 259!
If the sum total is one less or one greater than 259 it all works fine! On other developers machine the total path length was less than or greater than 259 but was never equal to 259 and because of this reason we were all able to build the project but she was not able to build the project on her machine! Weird isn't?
This issue occurs due to a bug in the Path.GetFullPath in .NET Framework library. This is a known issue in Visual Studio 2010.
The Fix:
The workaround is pretty straight forward, rename one of the folders a bit longer or a bit shorter and make sure that, the sum of the two path length is NOT equal to 259 chars.
For more information on the issue visit the offical Microsoft Knowledge base article here http://support.microsoft.com/kb/2516078
Now you know, we are having so much fun with .Net (sarcasm intended)!
Saturday, May 14, 2011
Fixing the "skia decoder->decode returned false" error while downloading images on Android
I had to implement a common requirement in one of my Android project. I had to show a image that was hosted on the web, in my Android application. Now that is a very common situation right? I am sure you must have faced this situation before.
I would have imagined, showing the images from the web would be as simple as setting the correct image source URL to the ImageView. But as it turns out life is not always that simple.
So how do we show the images from the web using the ImageView?
The above code works and works well, it does show the image hosted on the web in the ImageView. However, sometimes, I get an error in the log saying
Now, this is something unexpected. Whenever I get this error the image download stops and image is not shown in the ImageView.
The Cause:
Googled a little and as usual, I was not the only one to face this issue. There are lots of threads online, that mention this particular issue (here and here), "decoder returned false".
It seems that when skip method of the InputStream class does not skip the given number of bytes then, the BitmapFactory fails to decode the InputStream. Usually, the issue occurs on a slow internet connection. However, I cant confirm that, I have a 2Mbps dedicated internet connection, even on this connection issue is reproduicable!
The Fix:
There are two ways of fixing this issue.
Method 1 - Method that involves writing some custom code
This method makes sure that, the skip method of InputStream actually skip's the mentioned number of bytes.
To do this, we will need to wrap the InputStream into a custom class, override the skip method and ensure that we skip the given number of bytes no matter what. This can be achieved by subclassing the FilterInputStream class and overriding the skip method. Lets look at how the new implementation of the skip method.
As seen in the above code, the skip method simply makes sure that we skip the give number of bytes. The class FlushedInputStream now needs to be used while we decode the image. The updated code with the FilteredInputStream looks like
Method 2 - Method that involves using the BufferedInputStream
This method is very simple and straight forward we simply need to wrap the imageContentInputStream with the BufferedInputStream. This also seems to fix the problem. The updated code looks like
Yep, these two methods fix the "decode returned false" issue.
I would have imagined, showing the images from the web would be as simple as setting the correct image source URL to the ImageView. But as it turns out life is not always that simple.
So how do we show the images from the web using the ImageView?
- We have to first download the image using the HttpClient
- Decode the image data using BitmapFactory and convert it into Bitmap
- Associate the Decoded Bitmap with the ImageView
The above code works and works well, it does show the image hosted on the web in the ImageView. However, sometimes, I get an error in the log saying
Now, this is something unexpected. Whenever I get this error the image download stops and image is not shown in the ImageView.
The Cause:
Googled a little and as usual, I was not the only one to face this issue. There are lots of threads online, that mention this particular issue (here and here), "decoder returned false".
It seems that when skip method of the InputStream class does not skip the given number of bytes then, the BitmapFactory fails to decode the InputStream. Usually, the issue occurs on a slow internet connection. However, I cant confirm that, I have a 2Mbps dedicated internet connection, even on this connection issue is reproduicable!
The Fix:
There are two ways of fixing this issue.
Method 1 - Method that involves writing some custom code
This method makes sure that, the skip method of InputStream actually skip's the mentioned number of bytes.
To do this, we will need to wrap the InputStream into a custom class, override the skip method and ensure that we skip the given number of bytes no matter what. This can be achieved by subclassing the FilterInputStream class and overriding the skip method. Lets look at how the new implementation of the skip method.
As seen in the above code, the skip method simply makes sure that we skip the give number of bytes. The class FlushedInputStream now needs to be used while we decode the image. The updated code with the FilteredInputStream looks like
Method 2 - Method that involves using the BufferedInputStream
This method is very simple and straight forward we simply need to wrap the imageContentInputStream with the BufferedInputStream. This also seems to fix the problem. The updated code looks like
Yep, these two methods fix the "decode returned false" issue.
Saturday, April 30, 2011
How to fix the “circular file references are not allowed” Error in ASP.Net
On one of my .Net projects, I came across an interesting issue. I wasted couple of hours fixing it. Hence, I decided to share my findings, so that others do not have to waste their time fixing the same issue.
The issue
The issue was pretty simple, the app would not build. The error that I was getting while building a ASP.Net web project using MSBuild was:
Of course the error said there is some sort of circular reference in my code. I looked around to check and recheck, if I have created a circular reference by mistake. However, if there was any circular reference, the code would not compile. Code was compiling fine but it was failing when we ran aspnet_compiler.exe.
The ASP.Net Compilation tool (aspnet_compiler.exe) enables you to compile an ASP.Net Web application, this helps application performance because end users do not encounter a delay on the first request to the application.
I checked again, but certainly there was no code related circular dependency then why the aspnet_compiler.exe was complaining about the circular file references?
The Explanation
Googling a little, I found that, by default, in a Website Project, ASP.Net creates one DLL per folder. Hence, if you have the following setup:
- User control ucA.ascx is present in the directory "A". ucA.ascx refers another user control ucB.ascx
- User control ucB.ascx is present in the directory "B". ucB.ascx refers another user control ucC.ascx
- User control ucC.ascx is present in the directory "A".
The folder A's DLL will reference the folder B's DLL, which will again reference the folder A's DLL, causing a "circular file reference".
This is the reason why the aspnet_compiler.exe fails with the "circular file reference" error.
The Fix
There are two ways this issue could be fixed
- Rearrange the user controls (or MasterPages) to remove the circular references. Usually this means moving the user controls in separate directories. In our example, moving ucC.ascx to a new directory "C" (Preferred Solution).
- Use batch=”false” in the compilation tag of the web.config file. This will cause a new DLL to be created for each control/page in the site. This should fix the error but is really lousy for performance, so it should be avoided.
I moved the ucC.ascx in a different directory and yes the error went away!
Saturday, February 19, 2011
Android - Fixing the no internet connection issue on emulator
Recently, while developing an Android Application, I was faced with a situation that wasted 2 hours of my time. The issue was simple, my app was not able to access internet from the Android Emulator.
Initially I thought fixing the issue should have been straight forward, but life is not always that simple.
So what was the real issue because of which my app was not able to access internet? There are multiple reasons because of which this issue could occur. Hence, I decided to document my findings so that other could benefit from it.
There are two main symptoms of no internet connection on android emulator
A sample AndroidManifest.xml file with the added permission would look like
Compile and re-install the app in the emulator and try to access the internet from your app. It should work!
What is the use of uses-permission tag:
Android application can request certain permissions so that they can function properly. Some examples of permissions are, get users location, make a call, access the internet etc. App has to explicitly specify this in the AndroidManifest.xml.
When end users install such an app on their device, the android OS will notify the user that, app is requesting certain permissions. If users are fine with that, then only the app will be installed. Else users can deny the permission and the app will not be installed.
Configuring the proxy server on the emulator:
-dns-server takes a comma separated list of IP address to use as DNS server.
That did the trick for me! I was finally able to access internet from my app on the android emulator!
Initially I thought fixing the issue should have been straight forward, but life is not always that simple.
So what was the real issue because of which my app was not able to access internet? There are multiple reasons because of which this issue could occur. Hence, I decided to document my findings so that other could benefit from it.
There are two main symptoms of no internet connection on android emulator
- Only your app is not able to access the internet
- None of the apps are able to access the internet
Lets look at what are the reasons behind each of them.
Only your app is not able to access the internet
If only your app is not able to access the internet on the emulator, check if any other apps are able to access the internet or not. For e.g. you could open up the browser application, visit http://news.google.com/. If the page opens up correctly then problem lies in your app itself and its simple to fix.
Basically, your app needs the Permission to access the internet. This can be done by adding the following line just before the end of <manifest> tag in AndroidManifest.xml file of your application.
A sample AndroidManifest.xml file with the added permission would look like
Compile and re-install the app in the emulator and try to access the internet from your app. It should work!
What is the use of uses-permission tag:
Android application can request certain permissions so that they can function properly. Some examples of permissions are, get users location, make a call, access the internet etc. App has to explicitly specify this in the AndroidManifest.xml.
When end users install such an app on their device, the android OS will notify the user that, app is requesting certain permissions. If users are fine with that, then only the app will be installed. Else users can deny the permission and the app will not be installed.
This mechanism is Androids way of implementing security and users privacy!
None of the apps are able to access the internet
Now this situation is tricky. There are two reasons because of which this could happen
- Proxy server is not configured on the emulator
- Incorrect DNS used by the emulator
Configuring the proxy server on the emulator:
If you access internet via a proxy server, then you will need to configure the proxy server on the emulator as well. This is done via the following steps.
Open up the Settings application on the Emulator
![]() |
| Settings |
Select the Wireless & Network settings
![]() |
| Wireless and Networks |
Select the Mobile networks
![]() |
| Mobile Networks |
Select the Access Point Names
![]() |
| Access Point Names |
Select the Access Point listed there. In my case its Telkila
![]() |
| Select the access point |
In the Edit access point section edit the value of Proxy and Port. These should point to your Proxy server and Port that you use.
![]() |
| Set the proxy and port values |
Thats it! After doing these settings try to access the internet from the browser application on the emulator. It should work!
If you are not behind a proxy (like me) then the problem is because of an incorrect dns server. Lets look at how to fix the dns issue.
Incorrect DNS Used by the Emulator:
In my case none of the above things fixed the issue. The real issue was that the DNS used by the Emulator was incorrect. Fortunately there is a way in which we can specify which DNS server to use.
Android Emulator takes a command line parameter -dns-server using which you can specify which DNS server to use. The sample command to specify DNS server would look like
-dns-server takes a comma separated list of IP address to use as DNS server.
That did the trick for me! I was finally able to access internet from my app on the android emulator!
Labels:
access point,
android,
android emulator,
DNS,
fixing,
howto,
no internet,
proxy settings,
uses-permission
Saturday, July 17, 2010
How to fix the OutOfMemoryError when writing tests with PowerMock - Part - 2
In my previous post we had a look at How to fix the OutOfMemoryError when writing tests with PowerMock
Looks like, the method I described in that post does not work on all machine or environments.
In one of the environment whose configuration is:
Now that's annoying, what the hell is wrong now! Well as I said in the previous post memory leaks are hard to find and fix!
But we need a solution now! Can't wait till PowerMock 1.4 version is released. Hence, I was on a mission to fix the issue and get all my tests running by hook or crook!
In the process I found that, running the tests on a 32 bit machine had no problems what so ever! Surprise, Surprise!
Investigated a little further, found that when tests are run on a machine with 64 bit OS but 32 bit JDK they all pass. That was the moment of Truth!
These are the steps I followed to fix the OutOfMemoryError:
Looks like, the method I described in that post does not work on all machine or environments.
In one of the environment whose configuration is:
- Windows 7 64 Bit
- 4 GB RAM DDR3
- Intel Core i7 processor
- JDK 6 64 Bit
- PowerMock 1.3.7
Now that's annoying, what the hell is wrong now! Well as I said in the previous post memory leaks are hard to find and fix!
But we need a solution now! Can't wait till PowerMock 1.4 version is released. Hence, I was on a mission to fix the issue and get all my tests running by hook or crook!
In the process I found that, running the tests on a 32 bit machine had no problems what so ever! Surprise, Surprise!
Investigated a little further, found that when tests are run on a machine with 64 bit OS but 32 bit JDK they all pass. That was the moment of Truth!
These are the steps I followed to fix the OutOfMemoryError:
- Installed the JDK 6 32 bit on my machine
- Updated the JAVA_HOME environment variable
- Added the JDK 6 32 bit bin directory to the PATH environment variable
- Removed the earlier JDK 6 64 bit bin directory from the PATH environment variable.
Saturday, July 3, 2010
How to fix the OutOfMemoryError when writing tests with PowerMock
We use PowerMock (With the Mockito Api) in our current project as the Mocking/Stubbing framework. It has worked our really well so far!
Completely in love with the Mockito syntax and especially the error messages that it gives out when something is wrong.
Love the power that PowerMock adds to Mockito which enables us to test the un-testable code. The combination i.e. PowerMocks with Mockito, makes it a deadly combination to ignore and I call it Mocks on Steroids!
But life is not always perfect! is it? Recently I faced a situation, suddenly out of no where I started getting the
infamous java.lang.OutOfMemoryError: PermGen space Error here is the stack trace.
Man! its always a challenge to fix such issues.
Googled a little, found that its a known issue have a look at this url
Its caused because of a Memory leak (wow! as if I am telling something new! Its obvious, we get OutOfMemoryError when we have memory leaks! Everyone knows that!). Yes, and as you guys already know finding the source of memory leak is a challenge in itself.
But looks like, they have narrowed the problem down to the MockClassLoader class.
The problem:
If your project uses third party frameworks like log4j or spring, then those classes are also loaded by the MockClassLoader class. Causing log4j or spring classes to be loaded multiple times by multiple instances of MockClassLoader class.
The long term solution:
This problem will be fixed in the PowerMock's 1.4 version for sure!
But what should you do if you can't wait till then?
The short term solution:
Use the @PowerMockIgnore annotation. This annotation signal PowerMock not to load third party framework jars such as log4j and spring using the MockClassLoader. Let the system class loader load them.
The sample test case looks like this
That fixes the problem for our project!
This problem is a perfect example of the fact that, there is always a flip side!
But, if the flip side is not as bad, then as said by Amir Khan in the movie 3 idiots
All izz well!
Completely in love with the Mockito syntax and especially the error messages that it gives out when something is wrong.
Love the power that PowerMock adds to Mockito which enables us to test the un-testable code. The combination i.e. PowerMocks with Mockito, makes it a deadly combination to ignore and I call it Mocks on Steroids!
But life is not always perfect! is it? Recently I faced a situation, suddenly out of no where I started getting the
infamous java.lang.OutOfMemoryError: PermGen space Error here is the stack trace.
Man! its always a challenge to fix such issues.
Googled a little, found that its a known issue have a look at this url
Its caused because of a Memory leak (wow! as if I am telling something new! Its obvious, we get OutOfMemoryError when we have memory leaks! Everyone knows that!). Yes, and as you guys already know finding the source of memory leak is a challenge in itself.
But looks like, they have narrowed the problem down to the MockClassLoader class.
The problem:
If your project uses third party frameworks like log4j or spring, then those classes are also loaded by the MockClassLoader class. Causing log4j or spring classes to be loaded multiple times by multiple instances of MockClassLoader class.
The long term solution:
This problem will be fixed in the PowerMock's 1.4 version for sure!
But what should you do if you can't wait till then?
The short term solution:
Use the @PowerMockIgnore annotation. This annotation signal PowerMock not to load third party framework jars such as log4j and spring using the MockClassLoader. Let the system class loader load them.
The sample test case looks like this
That fixes the problem for our project!
This problem is a perfect example of the fact that, there is always a flip side!
But, if the flip side is not as bad, then as said by Amir Khan in the movie 3 idiots
All izz well!
Subscribe to:
Posts (Atom)






