Bael 2019 static vs dynamic binding (#4807)
* adding the required classes for binding example * completed test class for AnimalActivityUnitTest static functions * changes to example so that they don't seem to be a replica of https://stackoverflow.com/questions/19017258/static-vs-dynamic-binding-in-java * refactoring and removing unnecessary file * revert changes to RegexUnitTest
This commit is contained in:
parent
b3579151a9
commit
99676b1a9e
|
@ -0,0 +1,23 @@
|
||||||
|
package com.baeldung.binding;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by madhumita.g on 25-07-2018.
|
||||||
|
*/
|
||||||
|
public class Animal {
|
||||||
|
|
||||||
|
final static Logger logger = LoggerFactory.getLogger(Animal.class);
|
||||||
|
|
||||||
|
public void makeNoise() {
|
||||||
|
logger.info("generic animal noise");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void makeNoise(Integer repetitions) {
|
||||||
|
while(repetitions != 0) {
|
||||||
|
logger.info("generic animal noise countdown " + repetitions);
|
||||||
|
repetitions -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.binding;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by madhumita.g on 25-07-2018.
|
||||||
|
*/
|
||||||
|
public class AnimalActivity {
|
||||||
|
|
||||||
|
final static Logger logger = LoggerFactory.getLogger(AnimalActivity.class);
|
||||||
|
|
||||||
|
|
||||||
|
public static void sleep(Animal animal) {
|
||||||
|
logger.info("Animal is sleeping");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void sleep(Cat cat) {
|
||||||
|
logger.info("Cat is sleeping");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
Animal animal = new Animal();
|
||||||
|
|
||||||
|
//calling methods of animal object
|
||||||
|
animal.makeNoise();
|
||||||
|
|
||||||
|
animal.makeNoise(3);
|
||||||
|
|
||||||
|
|
||||||
|
//assigning a dog object to reference of type Animal
|
||||||
|
Animal catAnimal = new Cat();
|
||||||
|
|
||||||
|
catAnimal.makeNoise();
|
||||||
|
|
||||||
|
// calling static function
|
||||||
|
AnimalActivity.sleep(catAnimal);
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.binding;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by madhumita.g on 25-07-2018.
|
||||||
|
*/
|
||||||
|
public class Cat extends Animal {
|
||||||
|
|
||||||
|
final static Logger logger = LoggerFactory.getLogger(Cat.class);
|
||||||
|
|
||||||
|
public void makeNoise() {
|
||||||
|
|
||||||
|
logger.info("meow");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,95 @@
|
||||||
|
package com.baeldung.binding;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.Level;
|
||||||
|
import ch.qos.logback.classic.Logger;
|
||||||
|
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||||
|
import ch.qos.logback.core.Appender;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*https://gist.github.com/bloodredsun/a041de13e57bf3c6c040
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
|
||||||
|
public class AnimalActivityUnitTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Appender mockAppender;
|
||||||
|
@Captor
|
||||||
|
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
|
logger.addAppender(mockAppender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void teardown() {
|
||||||
|
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
|
logger.detachAppender(mockAppender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnimalReference__whenRefersAnimalObject_shouldCallFunctionWithAnimalParam() {
|
||||||
|
|
||||||
|
Animal animal = new Animal();
|
||||||
|
|
||||||
|
AnimalActivity.sleep(animal);
|
||||||
|
|
||||||
|
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||||
|
|
||||||
|
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getFormattedMessage(),
|
||||||
|
is("Animal is sleeping"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDogReference__whenRefersCatObject_shouldCallFunctionWithAnimalParam() {
|
||||||
|
|
||||||
|
Cat cat = new Cat();
|
||||||
|
|
||||||
|
AnimalActivity.sleep(cat);
|
||||||
|
|
||||||
|
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||||
|
|
||||||
|
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getFormattedMessage(),
|
||||||
|
is("Cat is sleeping"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAnimaReference__whenRefersDogObject_shouldCallFunctionWithAnimalParam() {
|
||||||
|
|
||||||
|
Animal cat = new Cat();
|
||||||
|
|
||||||
|
AnimalActivity.sleep(cat);
|
||||||
|
|
||||||
|
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||||
|
|
||||||
|
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getFormattedMessage(),
|
||||||
|
is("Animal is sleeping"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package com.baeldung.binding;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.Level;
|
||||||
|
import ch.qos.logback.classic.Logger;
|
||||||
|
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||||
|
import ch.qos.logback.core.Appender;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by madhumita.g on 01-08-2018.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class AnimalUnitTest {
|
||||||
|
@Mock
|
||||||
|
private Appender mockAppender;
|
||||||
|
@Captor
|
||||||
|
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
|
logger.addAppender(mockAppender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void teardown() {
|
||||||
|
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
|
logger.detachAppender(mockAppender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalledWithoutParameters_shouldCallFunctionMakeNoiseWithoutParameters() {
|
||||||
|
|
||||||
|
Animal animal = new Animal();
|
||||||
|
|
||||||
|
animal.makeNoise();
|
||||||
|
|
||||||
|
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||||
|
|
||||||
|
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getFormattedMessage(),
|
||||||
|
is("generic animal noise"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenCalledWithParameters_shouldCallFunctionMakeNoiseWithParameters() {
|
||||||
|
|
||||||
|
Animal animal = new Animal();
|
||||||
|
|
||||||
|
int testValue = 3;
|
||||||
|
animal.makeNoise(testValue);
|
||||||
|
|
||||||
|
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||||
|
|
||||||
|
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||||
|
|
||||||
|
while (testValue != 0) {
|
||||||
|
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getFormattedMessage(),
|
||||||
|
is("generic animal noise countdown 3\n"
|
||||||
|
+ "generic animal noise countdown 2\n"
|
||||||
|
+ "generic animal noise countdown 1\n"));
|
||||||
|
|
||||||
|
testValue-=1;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.baeldung.binding;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.Level;
|
||||||
|
import ch.qos.logback.classic.Logger;
|
||||||
|
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||||
|
import ch.qos.logback.core.Appender;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Captor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by madhumita.g on 01-08-2018.
|
||||||
|
*/
|
||||||
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
|
public class CatUnitTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Appender mockAppender;
|
||||||
|
|
||||||
|
@Captor
|
||||||
|
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
|
logger.addAppender(mockAppender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void teardown() {
|
||||||
|
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
|
||||||
|
logger.detachAppender(mockAppender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void makeNoiseTest() {
|
||||||
|
|
||||||
|
Cat cat = new Cat();
|
||||||
|
|
||||||
|
cat.makeNoise();
|
||||||
|
|
||||||
|
verify(mockAppender).doAppend(captorLoggingEvent.capture());
|
||||||
|
|
||||||
|
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||||
|
|
||||||
|
assertThat(loggingEvent.getFormattedMessage(),
|
||||||
|
is("meow"));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue