Move articles out of core-java-lang part 3
This commit is contained in:
@@ -9,4 +9,9 @@ This module contains articles about Object-oriented programming (OOP) in Java
|
||||
- [Guide to the this Java Keyword](https://www.baeldung.com/java-this)
|
||||
- [Java Public Access Modifier](https://www.baeldung.com/java-public-keyword)
|
||||
- [Composition, Aggregation and Association in Java](https://www.baeldung.com/java-composition-aggregation-association)
|
||||
- [Nested Classes in Java](https://www.baeldung.com/java-nested-classes)
|
||||
- [A Guide to Inner Interfaces in Java](https://www.baeldung.com/java-inner-interfaces)
|
||||
- [Java Classes and Objects](https://www.baeldung.com/java-classes-objects)
|
||||
- [Java Interfaces](https://www.baeldung.com/java-interfaces)
|
||||
- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-oop-2)
|
||||
@@ -1,7 +1,6 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-lang-oop-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-lang-oop-3</name>
|
||||
@@ -15,6 +14,24 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- logging -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
@@ -23,4 +40,18 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-lang-oop-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -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,21 @@
|
||||
package com.baeldung.innerinterfaces;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CommaSeparatedCustomers implements Customer.List {
|
||||
|
||||
private List<Customer> customers = new ArrayList<Customer>();
|
||||
|
||||
@Override
|
||||
public void Add(Customer customer) {
|
||||
customers.add(customer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCustomerNames() {
|
||||
return customers.stream().map(customer -> customer.getName()).collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.innerinterfaces;
|
||||
|
||||
public class Customer {
|
||||
public interface List {
|
||||
void Add(Customer customer);
|
||||
|
||||
String getCustomerNames();
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
public Customer(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.interfaces;
|
||||
|
||||
public interface Box extends HasColor {
|
||||
int getHeight();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.interfaces;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private double salary;
|
||||
|
||||
public double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
|
||||
public void setSalary(double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.interfaces;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class EmployeeSalaryComparator implements Comparator<Employee> {
|
||||
|
||||
@Override
|
||||
public int compare(Employee employeeA, Employee employeeB) {
|
||||
if (employeeA.getSalary() < employeeB.getSalary()) {
|
||||
return -1;
|
||||
} else if (employeeA.getSalary() > employeeB.getSalary()) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.interfaces;
|
||||
|
||||
public interface HasColor {
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.interfaces.multiinheritance;
|
||||
|
||||
public class Car implements Fly,Transform {
|
||||
|
||||
@Override
|
||||
public void fly() {
|
||||
System.out.println("I can Fly!!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform() {
|
||||
System.out.println("I can Transform!!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.interfaces.multiinheritance;
|
||||
|
||||
public interface Fly {
|
||||
|
||||
void fly();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.interfaces.multiinheritance;
|
||||
|
||||
public interface Transform {
|
||||
|
||||
void transform();
|
||||
|
||||
default void printSpecs(){
|
||||
System.out.println("Transform Specification");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.interfaces.multiinheritance;
|
||||
|
||||
public abstract class Vehicle implements Transform {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.interfaces.polymorphysim;
|
||||
|
||||
public class Circle implements Shape {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "Circle";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.interfaces.polymorphysim;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MainTestClass {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<Shape> shapes = new ArrayList<>();
|
||||
Shape circleShape = new Circle();
|
||||
Shape squareShape = new Square();
|
||||
|
||||
shapes.add(circleShape);
|
||||
shapes.add(squareShape);
|
||||
|
||||
for (Shape shape : shapes) {
|
||||
System.out.println(shape.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.interfaces.polymorphysim;
|
||||
|
||||
public interface Shape {
|
||||
|
||||
String name();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.interfaces.polymorphysim;
|
||||
|
||||
public class Square implements Shape {
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "Square";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.objects;
|
||||
|
||||
public class Car {
|
||||
|
||||
private String type;
|
||||
private String model;
|
||||
private String color;
|
||||
private int speed;
|
||||
|
||||
public Car(String type, String model, String color) {
|
||||
this.type = type;
|
||||
this.model = model;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public int increaseSpeed(int increment) {
|
||||
if (increment > 0) {
|
||||
this.speed += increment;
|
||||
} else {
|
||||
System.out.println("Increment can't be negative.");
|
||||
}
|
||||
return this.speed;
|
||||
}
|
||||
|
||||
public int decreaseSpeed(int decrement) {
|
||||
if (decrement > 0 && decrement <= this.speed) {
|
||||
this.speed -= decrement;
|
||||
} else {
|
||||
System.out.println("Decrement can't be negative or greater than current speed.");
|
||||
}
|
||||
return this.speed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Car [type=" + type + ", model=" + model + ", color=" + color + ", speed=" + speed + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class NonPrimitives {
|
||||
public static void main(String[] args) {
|
||||
FooClass a = new FooClass(1);
|
||||
FooClass b = new FooClass(1);
|
||||
|
||||
System.out.printf("Before Modification: a = %d and b = %d ", a.num, b.num);
|
||||
modify(a, b);
|
||||
System.out.printf("\nAfter Modification: a = %d and b = %d ", a.num, b.num);
|
||||
}
|
||||
|
||||
public static void modify(FooClass a1, FooClass b1) {
|
||||
a1.num++;
|
||||
|
||||
b1 = new FooClass(1);
|
||||
b1.num++;
|
||||
}
|
||||
}
|
||||
|
||||
class FooClass {
|
||||
public int num;
|
||||
|
||||
public FooClass(int num) {
|
||||
this.num = num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class Primitives {
|
||||
public static void main(String[] args) {
|
||||
int x = 1;
|
||||
int y = 2;
|
||||
|
||||
System.out.printf("Before Modification: x = %d and y = %d ", x, y);
|
||||
modify(x, y);
|
||||
System.out.printf("\nAfter Modification: x = %d and y = %d ", x, y);
|
||||
}
|
||||
|
||||
public static void modify(int x1, int y1) {
|
||||
x1 = 5;
|
||||
y1 = 10;
|
||||
}
|
||||
}
|
||||
@@ -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,87 @@
|
||||
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 java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
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,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--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.innerinterfaces;
|
||||
|
||||
import com.baeldung.innerinterfaces.CommaSeparatedCustomers;
|
||||
import com.baeldung.innerinterfaces.Customer;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class InnerInterfaceUnitTest {
|
||||
@Test
|
||||
public void whenCustomerListJoined_thenReturnsJoinedNames() {
|
||||
Customer.List customerList = new CommaSeparatedCustomers();
|
||||
customerList.Add(new Customer("customer1"));
|
||||
customerList.Add(new Customer("customer2"));
|
||||
assertEquals("customer1,customer2", customerList.getCustomerNames());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
abstract class SimpleAbstractClass {
|
||||
abstract void run();
|
||||
}
|
||||
|
||||
public class AnonymousInner {
|
||||
|
||||
@Test
|
||||
public void run() {
|
||||
SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() {
|
||||
void run() {
|
||||
System.out.println("Running Anonymous Class...");
|
||||
}
|
||||
};
|
||||
simpleAbstractClass.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Enclosing {
|
||||
|
||||
private static int x = 1;
|
||||
|
||||
public static class StaticNested {
|
||||
|
||||
private void run() {
|
||||
System.out.println("x = " + x);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
StaticNested nested = new StaticNested();
|
||||
nested.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NewEnclosing {
|
||||
|
||||
private void run() {
|
||||
class Local {
|
||||
void run() {
|
||||
System.out.println("Welcome to Baeldung!");
|
||||
}
|
||||
}
|
||||
Local local = new Local();
|
||||
local.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
NewEnclosing newEnclosing = new NewEnclosing();
|
||||
newEnclosing.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NewOuter {
|
||||
|
||||
int a = 1;
|
||||
static int b = 2;
|
||||
|
||||
public class InnerClass {
|
||||
int a = 3;
|
||||
static final int b = 4;
|
||||
|
||||
public void run() {
|
||||
System.out.println("a = " + a);
|
||||
System.out.println("b = " + b);
|
||||
System.out.println("NewOuter.this.a = " + NewOuter.this.a);
|
||||
System.out.println("NewOuter.b = " + NewOuter.b);
|
||||
System.out.println("NewOuter.this.b = " + NewOuter.this.b);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
NewOuter outer = new NewOuter();
|
||||
InnerClass inner = outer.new InnerClass();
|
||||
inner.run();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.nestedclass;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class Outer {
|
||||
|
||||
public class Inner {
|
||||
|
||||
public void run() {
|
||||
System.out.println("Calling test...");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Outer outer = new Outer();
|
||||
Inner inner = outer.new Inner();
|
||||
inner.run();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.objects;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ObjectsUnitTest {
|
||||
|
||||
private Car car;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
car = new Car("Ford", "Focus", "red");
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void when_speedIncreased_then_verifySpeed() {
|
||||
car.increaseSpeed(30);
|
||||
assertEquals(30, car.getSpeed());
|
||||
|
||||
car.increaseSpeed(20);
|
||||
assertEquals(50, car.getSpeed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void when_speedDecreased_then_verifySpeed() {
|
||||
car.increaseSpeed(50);
|
||||
assertEquals(50, car.getSpeed());
|
||||
|
||||
car.decreaseSpeed(30);
|
||||
assertEquals(20, car.getSpeed());
|
||||
|
||||
car.decreaseSpeed(20);
|
||||
assertEquals(0, car.getSpeed());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user