BAEL-1210 Guide to the Static Keyword in Java (#2767)
* Adding Source Files * Guide to the Static Keyword in Java - Test files
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CarIntegrationTest {
|
||||
@Test
|
||||
public void whenNumberOfCarObjectsInitialized_thenStaticCounterIncreases() {
|
||||
new Car("Jaguar", "V8");
|
||||
new Car("Bugatti", "W16");
|
||||
assertEquals(2, Car.numberOfCars);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SingletonIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenStaticInnerClass_whenMultipleTimesInstanceCalled_thenOnlyOneTimeInitialized() {
|
||||
Singleton object1 = Singleton.getInstance();
|
||||
Singleton object2 = Singleton.getInstance();
|
||||
|
||||
Assert.assertSame(object1, object2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.staticdemo;
|
||||
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StaticBlockIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenAddedListElementsThroughStaticBlock_thenEnsureCorrectOrder() {
|
||||
List<String> actualList = StaticBlock.getRanks();
|
||||
assertThat(actualList, contains("Lieutenant", "Captain", "Major", "Colonel", "General"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user