4- Fourth commit for core-java module splitting. This commit includes:
* to core-java-lang: * https://www.baeldung.com/java-stack-overflow-error * https://www.baeldung.com/java-new-custom-exception * https://www.baeldung.com/java-exceptions * https://www.baeldung.com/java-final-finally-finalize * https://www.baeldung.com/java-static-dynamic-binding * https://www.baeldung.com/java-throw-throws * https://www.baeldung.com/java-synthetic * https://www.baeldung.com/java-switch * https://www.baeldung.com/modulo-java * https://www.baeldung.com/java-ternary-operator
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
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"));
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
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.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 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,times(3)).doAppend(captorLoggingEvent.capture());
|
||||
|
||||
final List<LoggingEvent> loggingEvents = captorLoggingEvent.getAllValues();
|
||||
|
||||
for(LoggingEvent loggingEvent : loggingEvents)
|
||||
{
|
||||
assertThat(loggingEvent.getLevel(), is(Level.INFO));
|
||||
|
||||
assertThat(loggingEvent.getFormattedMessage(),
|
||||
is("generic animal noise countdown "+testValue));
|
||||
|
||||
testValue--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
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"));
|
||||
|
||||
}
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.customexception;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class IncorrectFileExtensionExceptionUnitTest {
|
||||
|
||||
@Test
|
||||
public void testWhenCorrectFileExtensionGiven_ReceivesNoException() throws IncorrectFileNameException {
|
||||
assertThat(FileManager.getFirstLine("correctFileNameWithProperExtension.txt")).isEqualTo("Default First Line");
|
||||
}
|
||||
|
||||
@Test(expected = IncorrectFileExtensionException.class)
|
||||
public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException {
|
||||
StringBuffer sBuffer = new StringBuffer();
|
||||
sBuffer.append("src");
|
||||
sBuffer.append(File.separator);
|
||||
sBuffer.append("test");
|
||||
sBuffer.append(File.separator);
|
||||
sBuffer.append("resources");
|
||||
sBuffer.append(File.separator);
|
||||
sBuffer.append("correctFileNameWithoutProperExtension");
|
||||
FileManager.getFirstLine(sBuffer.toString());
|
||||
}
|
||||
|
||||
}
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.customexception;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class IncorrectFileNameExceptionUnitTest {
|
||||
|
||||
@Test(expected = IncorrectFileNameException.class)
|
||||
public void testWhenIncorrectFileNameExceptionThrown_ReceivesIncorrectFileNameException() throws IncorrectFileNameException {
|
||||
FileManager.getFirstLine("wrongFileName.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhenCorrectFileNameExceptionThrown_ReceivesNoException() throws IncorrectFileNameException {
|
||||
assertThat(FileManager.getFirstLine("correctFileName.txt")).isEqualTo("Default First Line");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package com.baeldung.exceptionhandling;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.NoSuchFileException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
public class ExceptionsUnitTest {
|
||||
|
||||
Exceptions exceptions = new Exceptions();
|
||||
|
||||
@Test
|
||||
public void getPlayers() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayers())
|
||||
.isInstanceOf(NoSuchFileException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayers() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayers(""))
|
||||
.isInstanceOf(IllegalStateException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreThrows() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreThrows(""))
|
||||
.isInstanceOf(FileNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreTryCatch() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreTryCatch(""))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreFinally() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreFinally(""))
|
||||
.isInstanceOf(FileNotFoundException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersThrowingChecked() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersThrowingChecked(""))
|
||||
.isInstanceOf(TimeoutException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersThrowingUnchecked() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersThrowingUnchecked(""))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersWrapping() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersWrapping(""))
|
||||
.isInstanceOf(IOException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersRethrowing() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersRethrowing(""))
|
||||
.isInstanceOf(PlayerLoadException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadAllPlayersThrowable() {
|
||||
assertThatThrownBy(() -> exceptions.loadAllPlayersThrowable(""))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPlayerScoreSwallowingExceptionAntiPatternAlternative2() {
|
||||
assertThatThrownBy(() -> exceptions.getPlayerScoreSwallowingExceptionAntiPatternAlternative2(""))
|
||||
.isInstanceOf(PlayerScoreException.class);
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.baeldung.modulo;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Java6Assertions.*;
|
||||
|
||||
public class ModuloUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenIntegerDivision_thenLosesRemainder(){
|
||||
assertThat(11 / 4).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDoubleDivision_thenKeepsRemainder(){
|
||||
assertThat(11 / 4.0).isEqualTo(2.75);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModulo_thenReturnsRemainder(){
|
||||
assertThat(11 % 4).isEqualTo(3);
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenDivisionByZero_thenArithmeticException(){
|
||||
double result = 1 / 0;
|
||||
}
|
||||
|
||||
@Test(expected = ArithmeticException.class)
|
||||
public void whenModuloByZero_thenArithmeticException(){
|
||||
double result = 1 % 0;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){
|
||||
assertThat(3 % 2).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){
|
||||
assertThat(4 % 2).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){
|
||||
int QUEUE_CAPACITY= 10;
|
||||
int[] circularQueue = new int[QUEUE_CAPACITY];
|
||||
int itemsInserted = 0;
|
||||
for (int value = 0; value < 1000; value++) {
|
||||
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
|
||||
circularQueue[writeIndex] = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.stackoverflowerror;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AccountHolderManualTest {
|
||||
@Test(expected = StackOverflowError.class)
|
||||
public void whenInstanciatingAccountHolder_thenThrowsException() {
|
||||
AccountHolder holder = new AccountHolder();
|
||||
}
|
||||
}
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.stackoverflowerror;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CyclicDependancyManualTest {
|
||||
@Test(expected = StackOverflowError.class)
|
||||
public void whenInstanciatingClassOne_thenThrowsException() {
|
||||
ClassOne obj = new ClassOne();
|
||||
}
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.stackoverflowerror;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class InfiniteRecursionWithTerminationConditionManualTest {
|
||||
@Test
|
||||
public void givenPositiveIntNoOne_whenCalcFact_thenCorrectlyCalc() {
|
||||
int numToCalcFactorial = 1;
|
||||
InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition();
|
||||
|
||||
assertEquals(1, irtc.calculateFactorial(numToCalcFactorial));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPositiveIntGtOne_whenCalcFact_thenCorrectlyCalc() {
|
||||
int numToCalcFactorial = 5;
|
||||
InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition();
|
||||
|
||||
assertEquals(120, irtc.calculateFactorial(numToCalcFactorial));
|
||||
}
|
||||
|
||||
@Test(expected = StackOverflowError.class)
|
||||
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
|
||||
int numToCalcFactorial = -1;
|
||||
InfiniteRecursionWithTerminationCondition irtc = new InfiniteRecursionWithTerminationCondition();
|
||||
|
||||
irtc.calculateFactorial(numToCalcFactorial);
|
||||
}
|
||||
}
|
||||
-15
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.stackoverflowerror;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class RecursionWithCorrectTerminationConditionManualTest {
|
||||
@Test
|
||||
public void givenNegativeInt_whenCalcFact_thenCorrectlyCalc() {
|
||||
int numToCalcFactorial = -1;
|
||||
RecursionWithCorrectTerminationCondition rctc = new RecursionWithCorrectTerminationCondition();
|
||||
|
||||
assertEquals(1, rctc.calculateFactorial(numToCalcFactorial));
|
||||
}
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.stackoverflowerror;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class UnintendedInfiniteRecursionManualTest {
|
||||
@Test(expected = StackOverflowError.class)
|
||||
public void givenPositiveIntNoOne_whenCalFact_thenThrowsException() {
|
||||
int numToCalcFactorial = 1;
|
||||
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
||||
|
||||
uir.calculateFactorial(numToCalcFactorial);
|
||||
}
|
||||
|
||||
@Test(expected = StackOverflowError.class)
|
||||
public void givenPositiveIntGtOne_whenCalcFact_thenThrowsException() {
|
||||
int numToCalcFactorial = 2;
|
||||
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
||||
|
||||
uir.calculateFactorial(numToCalcFactorial);
|
||||
}
|
||||
|
||||
@Test(expected = StackOverflowError.class)
|
||||
public void givenNegativeInt_whenCalcFact_thenThrowsException() {
|
||||
int numToCalcFactorial = -1;
|
||||
UnintendedInfiniteRecursion uir = new UnintendedInfiniteRecursion();
|
||||
|
||||
uir.calculateFactorial(numToCalcFactorial);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.switchstatement;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
public class SwitchStatementUnitTest {
|
||||
private SwitchStatement s = new SwitchStatement();
|
||||
|
||||
|
||||
@Test
|
||||
public void whenDog_thenDomesticAnimal() {
|
||||
|
||||
String animal = "DOG";
|
||||
Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoBreaks_thenGoThroughBlocks() {
|
||||
String animal = "DOG";
|
||||
Assert.assertEquals("unknown animal", s.forgetBreakInSwitch(animal));
|
||||
}
|
||||
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void whenSwitchAgumentIsNull_thenNullPointerException() {
|
||||
String animal = null;
|
||||
Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenCompareStrings_thenByEqual() {
|
||||
String animal = new String("DOG");
|
||||
Assert.assertEquals("domestic animal", s.exampleOfSwitch(animal));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package com.baeldung.synthetic;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for {@link SyntheticFieldDemo}, {@link SyntheticMethodDemo},
|
||||
* {@link SyntheticConstructorDemo} and {@link BridgeMethodDemo} classes.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class SyntheticUnitTest {
|
||||
|
||||
/**
|
||||
* Tests that the {@link SyntheticMethodDemo.NestedClass} contains two synthetic
|
||||
* methods.
|
||||
*/
|
||||
@Test
|
||||
public void givenSyntheticMethod_whenIsSinthetic_thenTrue() {
|
||||
// Checks that the nested class contains exactly two synthetic methods.
|
||||
Method[] methods = SyntheticMethodDemo.NestedClass.class.getDeclaredMethods();
|
||||
Assert.assertEquals("This class should contain only two methods", 2, methods.length);
|
||||
|
||||
for (Method m : methods) {
|
||||
System.out.println("Method: " + m.getName() + ", isSynthetic: " + m.isSynthetic());
|
||||
Assert.assertTrue("All the methods of this class should be synthetic", m.isSynthetic());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that {@link SyntheticConstructorDemo.NestedClass} contains a synthetic
|
||||
* constructor.
|
||||
*/
|
||||
@Test
|
||||
public void givenSyntheticConstructor_whenIsSinthetic_thenTrue() {
|
||||
// Checks that the nested class contains exactly a synthetic
|
||||
// constructor.
|
||||
int syntheticConstructors = 0;
|
||||
Constructor<?>[] constructors = SyntheticConstructorDemo.NestedClass.class.getDeclaredConstructors();
|
||||
Assert.assertEquals("This class should contain only two constructors", 2, constructors.length);
|
||||
|
||||
for (Constructor<?> c : constructors) {
|
||||
System.out.println("Constructor: " + c.getName() + ", isSynthetic: " + c.isSynthetic());
|
||||
|
||||
// Counts the synthetic constructors.
|
||||
if (c.isSynthetic()) {
|
||||
syntheticConstructors++;
|
||||
}
|
||||
}
|
||||
|
||||
// Checks that there's exactly one synthetic constructor.
|
||||
Assert.assertEquals(1, syntheticConstructors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that {@link SyntheticFieldDemo.NestedClass} contains a synthetic field.
|
||||
*/
|
||||
@Test
|
||||
public void givenSyntheticField_whenIsSinthetic_thenTrue() {
|
||||
// This class should contain exactly one synthetic field.
|
||||
Field[] fields = SyntheticFieldDemo.NestedClass.class.getDeclaredFields();
|
||||
Assert.assertEquals("This class should contain only one field", 1, fields.length);
|
||||
|
||||
for (Field f : fields) {
|
||||
System.out.println("Field: " + f.getName() + ", isSynthetic: " + f.isSynthetic());
|
||||
Assert.assertTrue("All the fields of this class should be synthetic", f.isSynthetic());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that {@link BridgeMethodDemo} contains a synthetic bridge method.
|
||||
*/
|
||||
@Test
|
||||
public void givenBridgeMethod_whenIsBridge_thenTrue() {
|
||||
// This class should contain exactly one synthetic bridge method.
|
||||
int syntheticMethods = 0;
|
||||
Method[] methods = BridgeMethodDemo.class.getDeclaredMethods();
|
||||
for (Method m : methods) {
|
||||
System.out.println(
|
||||
"Method: " + m.getName() + ", isSynthetic: " + m.isSynthetic() + ", isBridge: " + m.isBridge());
|
||||
|
||||
// Counts the synthetic methods and checks that they are also bridge
|
||||
// methods.
|
||||
if (m.isSynthetic()) {
|
||||
syntheticMethods++;
|
||||
Assert.assertTrue("The synthetic method in this class should also be a bridge method", m.isBridge());
|
||||
}
|
||||
}
|
||||
|
||||
// Checks that there's exactly one synthetic bridge method.
|
||||
Assert.assertEquals("There should be exactly 1 synthetic bridge method in this class", 1, syntheticMethods);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package com.baeldung.ternaryoperator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TernaryOperatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenACondition_whenUsingTernaryOperator_thenItEvaluatesConditionAndReturnsAValue() {
|
||||
int number = 10;
|
||||
String msg = number > 10 ? "Number is greater than 10" : "Number is less than or equal to 10";
|
||||
|
||||
assertThat(msg).isEqualTo("Number is less than or equal to 10");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenATrueCondition_whenUsingTernaryOperator_thenOnlyExpression1IsEvaluated() {
|
||||
int exp1 = 0, exp2 = 0;
|
||||
int result = 12 > 10 ? ++exp1 : ++exp2;
|
||||
|
||||
assertThat(exp1).isEqualTo(1);
|
||||
assertThat(exp2).isEqualTo(0);
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAFalseCondition_whenUsingTernaryOperator_thenOnlyExpression2IsEvaluated() {
|
||||
int exp1 = 0, exp2 = 0;
|
||||
int result = 8 > 10 ? ++exp1 : ++exp2;
|
||||
|
||||
assertThat(exp1).isEqualTo(0);
|
||||
assertThat(exp2).isEqualTo(1);
|
||||
assertThat(result).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenANestedCondition_whenUsingTernaryOperator_thenCorrectValueIsReturned() {
|
||||
int number = 6;
|
||||
String msg = number > 10 ? "Number is greater than 10" : number > 5 ? "Number is greater than 5" : "Number is less than or equal to 5";
|
||||
|
||||
assertThat(msg).isEqualTo("Number is greater than 5");
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.throwsexception;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class SimpleServiceUnitTest {
|
||||
|
||||
SimpleService simpleService = new SimpleService();
|
||||
|
||||
@Test
|
||||
void whenSQLExceptionIsThrown_thenShouldBeRethrownWithWrappedException() {
|
||||
assertThrows(DataAccessException.class,
|
||||
() -> simpleService.wrappingException());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCalled_thenNullPointerExceptionIsThrown() {
|
||||
assertThrows(NullPointerException.class,
|
||||
() -> simpleService.runtimeNullPointerException());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user