Ali Arda Orhan
Ali Arda Orhan

Reputation: 784

JUnit Test on PropertyConfigurator.configure

I have been training myself on JUNIT testing. I want to test if configuration file is configured properly or not? I wrote a test that it should throw a FileNotFoundException. But when I run test although it throws exception, it passes test. What I want to design is, if PropertiesConfiguration.configure method throws a exception, it should fail test. My code is below:

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class isPropertiesOfLog4jExistAndConfiguredCorrectly {
    Logger log = null;
    File f=null;
    @Before
    public void setUp() throws Exception {
        log=Logger.getLogger(GuestBookExample.class.getName());
        f=new File("/web-inf/classes/log4j.properties");

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() {

        assertTrue("Configuration file is exist.",f!=null);
    }
    @Test
    public void test2(){
        PropertyConfigurator.configure("/....web-inf/classes/log4j.properties");

    }

}

Upvotes: 0

Views: 569

Answers (1)

Stefan Birkner
Stefan Birkner

Reputation: 24540

PropertyConfigurator configures your log4j. You cannot modify the behaviour of this class (e.g. how it handles failures). Hence you only can check whether the configuration is done properly. Try to verify some properties of the logger:

@Test
public void test2(){
  PropertyConfigurator.configure("/....web-inf/classes/log4j.properties");
  Logger log = Logger.getLogger(GuestBookExample.class.getName());
  assertEquals(Level.WARN, log.getLevel());
}

Upvotes: 0

Related Questions