Warning in JUnit tests

I want to show a warning in a JUnit test. I do not want the test to fail due to this warning, but I would like to know that this warning is occurring. Obviously, I can use System.err.print or something similar. Now I wonder how to do it right.

+7
source share
3 answers

I also thought it was useful from time to time, but it is not possible. JUnit insists on the right and wrong answer and therefore has only "success", "failure" and "error". Error during code break during test execution. I would reorganize and think if you really need a warning (or the log, like the previous answer, suggests, although I think this warning will be lost soon),

+5
source

use logger with console application for it

 final Logger logger = LoggerFactory.getLogger(LoggingTest.class); @Test public void test() { logger.warn("message"); } 

PS. An example of using slf4j logger.

+4
source

you may have a log4j configuration specific to your tests.

Step 1: create the src / test / resources / test-log4j.properties file containing the log4j configuration for your tests.

Example:

 log4j.rootLogger=DEBUG, R log4j.appender.R=org.apache.log4j.ConsoleAppender log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{3}:%L - %m%n 

Step 2: edit your pom.xml as follows:

 <build> ... <plugins> <plugin> <artifactid>maven-surefire-plugin</artifactid> <version>2.3</version> <configuration> <systemproperties> <property> <!-- Specific log4j config for tests --> <name>log4j.configuration</name> <value>test-log4j.properties</value> </property> </systemproperties> </configuration> </plugin> ... </plugins> </build> 

Now your log4j.properties is in src / main / webapp / WEB-INF / classes /

Hope this helps.

+2
source

All Articles