BAEL-3832: Removed logger checks from the test cases
This commit is contained in:
parent
5ad0170cb6
commit
2dd0e5001b
@ -1,16 +1,14 @@
|
|||||||
package com.baeldung.ioccontainer.bean;
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
import org.apache.log4j.LogManager;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
|
|
||||||
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||||
static final Logger LOGGER = LogManager.getLogger(CustomBeanPostProcessor.class.getName());
|
public static boolean isBeanInstantiated = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||||
LOGGER.info("BeanFactoryPostProcessor is Registered");
|
isBeanInstantiated = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
package com.baeldung.ioccontainer.bean;
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
import org.apache.log4j.LogManager;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
|
||||||
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
public class CustomBeanPostProcessor implements BeanPostProcessor {
|
||||||
|
public static boolean isBeanInstantiated = false;
|
||||||
static final Logger LOGGER = LogManager.getLogger(CustomBeanPostProcessor.class.getName());
|
|
||||||
|
@Override
|
||||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||||
LOGGER.info("BeanPostProcessor is Registered Before Initialization");
|
isBeanInstantiated = true;
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
package com.baeldung.ioccontainer.bean;
|
package com.baeldung.ioccontainer.bean;
|
||||||
|
|
||||||
import org.apache.log4j.LogManager;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
|
|
||||||
public class Student {
|
public class Student {
|
||||||
static final Logger LOGGER = LogManager.getLogger(Student.class.getName());
|
public static boolean isBeanInstantiated = false;
|
||||||
|
|
||||||
public void postConstruct() {
|
public void postConstruct() {
|
||||||
LOGGER.info("Student Bean is initialized");
|
isBeanInstantiated = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,6 @@ package com.baeldung.ioccontainer;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.log4j.AppenderSkeleton;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.apache.log4j.spi.LoggingEvent;
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@ -26,49 +20,37 @@ import com.baeldung.ioccontainer.bean.Student;
|
|||||||
|
|
||||||
public class IOCContainerAppUnitTest {
|
public class IOCContainerAppUnitTest {
|
||||||
|
|
||||||
private LogAppender logAppender;
|
|
||||||
private List<LoggingEvent> loggingEvents;
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void initializeLogAppender() {
|
|
||||||
logAppender = new LogAppender();
|
|
||||||
Logger.getRootLogger()
|
|
||||||
.addAppender(logAppender);
|
|
||||||
loggingEvents = logAppender.events;
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
public void removeLogAppender() {
|
public void resetInstantiationFlag() {
|
||||||
Logger.getRootLogger()
|
Student.isBeanInstantiated = false;
|
||||||
.removeAppender(logAppender);
|
CustomBeanPostProcessor.isBeanInstantiated = false;
|
||||||
|
CustomBeanFactoryPostProcessor.isBeanInstantiated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBFInitialized_thenNoStudentLogPrinted() {
|
public void whenBFInitialized_thenStudentNotInitialized() {
|
||||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
BeanFactory factory = new XmlBeanFactory(res);
|
BeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
|
||||||
String expected = "Student Bean is initialized";
|
assertFalse(Student.isBeanInstantiated);
|
||||||
assertFalse(checkWhetherLoggerContains(expected));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBFInitialized_thenStudentLogPrinted() {
|
public void whenBFInitialized_thenStudentInitialized() {
|
||||||
|
|
||||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
BeanFactory factory = new XmlBeanFactory(res);
|
BeanFactory factory = new XmlBeanFactory(res);
|
||||||
Student student = (Student) factory.getBean("student");
|
Student student = (Student) factory.getBean("student");
|
||||||
|
|
||||||
String expected = "Student Bean is initialized";
|
assertTrue(Student.isBeanInstantiated);
|
||||||
assertTrue(checkWhetherLoggerContains(expected));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAppContInitialized_thenStudentObjInitialized() {
|
public void whenAppContInitialized_thenStudentInitialized() {
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||||
|
|
||||||
String expected = "Student Bean is initialized";
|
assertTrue(Student.isBeanInstantiated);
|
||||||
assertTrue(checkWhetherLoggerContains(expected));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -76,22 +58,16 @@ public class IOCContainerAppUnitTest {
|
|||||||
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
Resource res = new ClassPathResource("ioc-container-difference-example.xml");
|
||||||
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
ConfigurableListableBeanFactory factory = new XmlBeanFactory(res);
|
||||||
|
|
||||||
String beanFactoryPostProcessorExpectedLog = "BeanFactoryPostProcessor is Registered";
|
assertFalse(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
||||||
assertFalse(checkWhetherLoggerContains(beanFactoryPostProcessorExpectedLog));
|
assertFalse(CustomBeanPostProcessor.isBeanInstantiated);
|
||||||
|
|
||||||
String beanPostProcessorExpectedLog = "BeanPostProcessor is Registered Before Initialization";
|
|
||||||
assertFalse(checkWhetherLoggerContains(beanPostProcessorExpectedLog));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenAppContInitialized_thenBFPostProcessorAndBPostProcessorRegisteredAutomatically() {
|
public void whenAppContInitialized_thenBFPostProcessorAndBPostProcessorRegisteredAutomatically() {
|
||||||
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");
|
||||||
|
|
||||||
String beanFactoryPostProcessorExpectedLog = "BeanFactoryPostProcessor is Registered";
|
assertTrue(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
||||||
assertTrue(checkWhetherLoggerContains(beanFactoryPostProcessorExpectedLog));
|
assertTrue(CustomBeanPostProcessor.isBeanInstantiated);
|
||||||
|
|
||||||
String beanPostProcessorExpectedLog = "BeanPostProcessor is Registered Before Initialization";
|
|
||||||
assertTrue(checkWhetherLoggerContains(beanPostProcessorExpectedLog));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -101,37 +77,12 @@ public class IOCContainerAppUnitTest {
|
|||||||
|
|
||||||
CustomBeanFactoryPostProcessor beanFactoryPostProcessor = new CustomBeanFactoryPostProcessor();
|
CustomBeanFactoryPostProcessor beanFactoryPostProcessor = new CustomBeanFactoryPostProcessor();
|
||||||
beanFactoryPostProcessor.postProcessBeanFactory(factory);
|
beanFactoryPostProcessor.postProcessBeanFactory(factory);
|
||||||
String beanFactoryPostProcessorExpectedLog = "BeanFactoryPostProcessor is Registered";
|
assertTrue(CustomBeanFactoryPostProcessor.isBeanInstantiated);
|
||||||
assertTrue(checkWhetherLoggerContains(beanFactoryPostProcessorExpectedLog));
|
|
||||||
|
|
||||||
CustomBeanPostProcessor beanPostProcessor = new CustomBeanPostProcessor();
|
CustomBeanPostProcessor beanPostProcessor = new CustomBeanPostProcessor();
|
||||||
factory.addBeanPostProcessor(beanPostProcessor);
|
factory.addBeanPostProcessor(beanPostProcessor);
|
||||||
Student student = (Student) factory.getBean("student");
|
Student student = (Student) factory.getBean("student");
|
||||||
String beanPostProcessorExpectedLog = "BeanPostProcessor is Registered Before Initialization";
|
assertTrue(CustomBeanPostProcessor.isBeanInstantiated);
|
||||||
assertTrue(checkWhetherLoggerContains(beanPostProcessorExpectedLog));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkWhetherLoggerContains(String expectedLogMessge) {
|
|
||||||
boolean isLogExist = loggingEvents.stream()
|
|
||||||
.anyMatch(logEvent -> logEvent.getMessage()
|
|
||||||
.equals(expectedLogMessge));
|
|
||||||
return isLogExist;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class LogAppender extends AppenderSkeleton {
|
|
||||||
public List<LoggingEvent> events = new ArrayList<LoggingEvent>();
|
|
||||||
|
|
||||||
public void close() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean requiresLayout() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void append(LoggingEvent event) {
|
|
||||||
events.add(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user