PowerMockIgnore and log4j

2,374 views
Skip to first unread message

Aaron

unread,
Aug 12, 2011, 7:17:45 PM8/12/11
to PowerMock
Hi,

I'm having an issue with log4j as it is not being ignored by the
@PowerMockIgnore("org.apache.log4j") as described in
http://code.google.com/p/powermock/wiki/FAQ the test do not fail just
pollute the test result reports.

General Info:
junit-4.8.1.jar
mockito-all-1.8.5.jar
powermock-mockito-1.4.9-full.jar
log4j-1.2.8.jar


Class with logger:

public class Manager extends OptionalDbTransactionDAO
{
private static final Logger log = Logger.getLogger(Manager.class);
...

Class under test:

public class SomeHelper {
public static String generateAllList(List complexes){
StringBuffer buffer = new StringBuffer();
Manager mgr = new Manager();
...

Test class:

@PowerMockIgnore("org.apache.log4j")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Manager.class})
public class QuotaHelperTest {

@Before
public void setUp() throws Exception {
mockMgr = mock(Manager.class);
// at this point the exceptions are trown

Console output:
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /home/oracle/folder/logs/application-
error.log (No such file or directory)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:192)
at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
at org.apache.log4j.FileAppender.setFile(FileAppender.java:272)
at
org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:
156)
at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:
151)
at
org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:
247)
at
org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:
123)
at
org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:
87)
at
org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:
645)
at
org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:
603)
at
org.apache.log4j.PropertyConfigurator.parseCatsAndRenderers(PropertyConfigurator.java:
524)
at
org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:
408)
at
org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:
432)
at
org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:
460)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:113)
at org.apache.log4j.Logger.getLogger(Logger.java:94)

Any clues on how to fix this?

tod...@gmail.com

unread,
Aug 12, 2011, 7:43:23 PM8/12/11
to powe...@googlegroups.com
Try "org.apache.log4j.*" - also, you may want to wrap it in curly braces (so that if you want to ignore any other classes, you can).

Hope that helps!

Todd
>        at java.io.FileOutputStream.(FileOutputStream.java:192)
>
>        at java.io.FileOutputStream.(FileOutputStream.java:116)
>        at org.apache.log4j.LogManager.(LogManager.java:113)

>
>        at org.apache.log4j.Logger.getLogger(Logger.java:94)
>
>
>
> Any clues on how to fix this?
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "PowerMock" group.
>
> To post to this group, send email to powe...@googlegroups.com.
>
> To unsubscribe from this group, send email to powermock+...@googlegroups.com.
>
> For more options, visit this group at http://groups.google.com/group/powermock?hl=en.
>
>
>

Aaron

unread,
Aug 15, 2011, 10:22:02 AM8/15/11
to PowerMock
Hi Todd,

I had it like that I think I removed it by mistake while taking off
the commons logging, however I'm still getting the log output in the
console. This is the actual annotation:
@PowerMockIgnore({"org.apache.log4j.*",
"org.apache.commons.logging.*"})

Thanks,
--Aaron

On Aug 12, 5:43 pm, todd...@gmail.com wrote:
> Try "org.apache.log4j.*" - also, you may want to wrap it in curly braces  
> (so that if you want to ignore any other classes, you can).
>
> Hope that helps!
>
> Todd
>
> On , Aaron <aaron.neva...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Hi,
> > I'm having an issue with log4j as it is not being ignored by the
> > @PowerMockIgnore("org.apache.log4j") as described in
> >http://code.google.com/p/powermock/wiki/FAQthe test do not fail just

Aaron

unread,
Aug 22, 2011, 3:44:29 PM8/22/11
to PowerMock
Hi All,

Since I was not able to make the @PowerMockIgnore annotation to work
with log4j, I tried a different approach and I will share with
everyone in case someone faces the same problem. Basically in order to
get around the issue I modified my ant script to basically shut up the
logger by specifying a different log4j property file to be used during
the junit excecution and the way you do this is by changing the
log4j.configuration system property in your junit ant task like this:

<junit fork="yes"
printsummary="yes"
haltonfailure="no"
showoutput="yes">
<sysproperty key="log4j.configuration" value="file:test$
{file.separator}test_log4j.properties"/>
....

Regards,
--Aaron.

On Aug 15, 8:22 am, Aaron <aaron.neva...@gmail.com> wrote:
> Hi Todd,
>
> I had it like that I think I removed it by mistake while taking off
> the commons logging, however I'm still getting the log output in the
> console. This is the actual annotation:
> @PowerMockIgnore({"org.apache.log4j.*",
> "org.apache.commons.logging.*"})
>
> Thanks,
> --Aaron
>
> On Aug 12, 5:43 pm, todd...@gmail.com wrote:
>
>
>
>
>
>
>
> > Try "org.apache.log4j.*" - also, you may want to wrap it in curly braces  
> > (so that if you want to ignore any other classes, you can).
>
> > Hope that helps!
>
> > Todd
>
> > On , Aaron <aaron.neva...@gmail.com> wrote:
>
> > > Hi,
> > > I'm having an issue with log4j as it is not being ignored by the
> > > @PowerMockIgnore("org.apache.log4j") as described in
> > >http://code.google.com/p/powermock/wiki/FAQthetest do not fail just

Johan Haleby

unread,
Aug 22, 2011, 5:53:58 PM8/22/11
to powe...@googlegroups.com
Thanks for sharing

/Johan

Reply all
Reply to author
Forward
0 new messages