Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit4 unit tests running inside eclipse, using java.util.logging - cannot see log output

Using Maven build system under eclipse.

I have just switched by project from using Apache Commons Logging to Java Utils Logging, as it has to live in an environment where Java Utils Logging is the main logger (Google App Engine), and other libraries that I use already use it (Restlet). One of the features I like in eclipse is to be able right click on a function thats marked with @test and select "Run as Junit Test". When I do this now, I see no logging output. I have created a file

src/test/java/resources/logging.properties

handlers = java.util.logging.ConsoleHandler

.level=INFO
my.great.package.level=FINE
java.util.logging.ConsoleHandler.level = FINE

But still I see nothing when running test inside Eclipse. Before it used to all work beautifully with commons logging and log4j. What am I doing wrong?

Thanks in advance,

like image 633
nwaltham Avatar asked Jan 09 '13 12:01

nwaltham


People also ask

How do I assert logs in JUnit?

Basically, you create your own Appender and add it to the logger you want. If you'd want to collect everything, the root logger is a good place to start, but you can use a more specific if you'd like. Don't forget to remove the Appender when you're done, otherwise you might create a memory leak.

How do you set the log level to debug in JUnit tests?

properties goes in the directory src/main/resources . My test version goes in src/test/resources . The Eclipse build path (classpath) is set up to search src/test/resources before src/main/resources , so your unit tests use the test file. The JAR (or WAR) build instructions use the files from src/main/resources .

Why my JUnit is not showing in Eclipse?

If you right click in Project Explorer and choose New, you see a much shorter menu that doesn't include the Junit option. Needs to be done in Package Explorer view...


1 Answers

I'm not sure if Java Logging can auto-detect configuration files in the same way that log4j can.

Have you tried specifying where the configuration file is for the test runner, e.g.:

-Djava.util.logging.config.file=path/to/logging.properties

You can add this to the system properties via the VM Arguments text box in the Eclipse Run Configurations dialog.

You can also configure the maven-surefire-plugin with the same system property so you get logging during your build - if desired.

like image 53
Jonathan Avatar answered Sep 25 '22 21:09

Jonathan