geroza
2018-11-10 01:21:37 -02:00
parent 64a22eec31
commit 61574b7228
46 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package com.baeldung.classnotfoundexception;
import org.junit.Test;
public class ClassNotFoundExceptionUnitTest {
@Test(expected = ClassNotFoundException.class)
public void givenNoDriversInClassPath_whenLoadDrivers_thenClassNotFoundException() throws ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.immutableobjects;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class ImmutableObjectsUnitTest {
@Test
public void whenCallingStringReplace_thenStringDoesNotMutate() {
// 2. What's an Immutable Object?
final String name = "baeldung";
final String newName = name.replace("dung", "----");
assertEquals("baeldung", name);
assertEquals("bael----", newName);
}
public void whenReassignFinalValue_thenCompilerError() {
// 3. The final Keyword in Java (1)
final String name = "baeldung";
// name = "bael...";
}
@Test
public void whenAddingElementToList_thenSizeChange() {
// 3. The final Keyword in Java (2)
final List<String> strings = new ArrayList<>();
assertEquals(0, strings.size());
strings.add("baeldung");
assertEquals(1, strings.size());
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.noclassdeffounderror;
import org.junit.Test;
public class NoClassDefFoundErrorUnitTest {
@Test(expected = NoClassDefFoundError.class)
public void givenInitErrorInClass_whenloadClass_thenNoClassDefFoundError() {
NoClassDefFoundErrorExample sample = new NoClassDefFoundErrorExample();
sample.getClassWithInitErrors();
}
}

View File

@@ -0,0 +1,37 @@
package com.baeldung.parameterpassing;
import org.junit.Assert;
import org.junit.Test;
public class NonPrimitivesUnitTest {
@Test
public void whenModifyingObjects_thenOriginalObjectChanged() {
Foo a = new Foo(1);
Foo b = new Foo(1);
// Before Modification
Assert.assertEquals(a.num, 1);
Assert.assertEquals(b.num, 1);
modify(a, b);
// After Modification
Assert.assertEquals(a.num, 2);
Assert.assertEquals(b.num, 1);
}
public static void modify(Foo a1, Foo b1) {
a1.num++;
b1 = new Foo(1);
b1.num++;
}
}
class Foo {
public int num;
public Foo(int num) {
this.num = num;
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.parameterpassing;
import org.junit.Assert;
import org.junit.Test;
public class PrimitivesUnitTest {
@Test
public void whenModifyingPrimitives_thenOriginalValuesNotModified() {
int x = 1;
int y = 2;
// Before Modification
Assert.assertEquals(x, 1);
Assert.assertEquals(y, 2);
modify(x, y);
// After Modification
Assert.assertEquals(x, 1);
Assert.assertEquals(y, 2);
}
public static void modify(int x1, int y1) {
x1 = 5;
y1 = 10;
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.system;
import org.junit.Assert;
import org.junit.Test;
public class DateTimeServiceUnitTest {
@Test
public void givenClass_whenCalledMethods_thenNotNullInResult() {
DateTimeService dateTimeService = new DateTimeService();
Assert.assertNotNull(dateTimeService.nowPlusOneHour());
Assert.assertNotNull(dateTimeService.nowPrettyPrinted());
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.system;
import org.junit.Assert;
import org.junit.Test;
public class EnvironmentVariablesUnitTest {
@Test
public void givenEnvVars_whenReadPath_thenGetValueinResult() {
EnvironmentVariables environmentVariables = new EnvironmentVariables();
Assert.assertNotNull(environmentVariables.getPath());
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.system;
import org.junit.Assert;
import org.junit.Test;
public class SystemArrayCopyUnitTest {
@Test
public void givenTwoArraysAB_whenUseArrayCopy_thenArrayCopiedFromAToBInResult() {
int[] a = {34, 22, 44, 2, 55, 3};
int[] b = new int[a.length];
// copy all elements from a to b
System.arraycopy(a, 0, b, 0, a.length);
Assert.assertArrayEquals(a, b);
}
@Test
public void givenTwoArraysAB_whenUseArrayCopyPosition_thenArrayCopiedFromAToBInResult() {
int[] a = {34, 22, 44, 2, 55, 3};
int[] b = new int[a.length];
// copy 2 elements from a, starting at a[1] to b, starting at b[3]
System.arraycopy(a, 1, b, 3, 2);
Assert.assertArrayEquals(new int[] {0, 0, 0, 22, 44, 0}, b);
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.system;
import org.junit.Assert;
import org.junit.Test;
public class SystemNanoUnitTest {
@Test
public void givenSystem_whenCalledNanoTime_thenGivesTimeinResult() {
long startTime = System.nanoTime();
// do something that takes time
long endTime = System.nanoTime();
Assert.assertTrue(endTime - startTime < 10000);
}
}

View File

@@ -0,0 +1,56 @@
package com.baeldung.system;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Properties;
public class SystemPropertiesUnitTest {
@Test
public void givenSystem_whenCalledGetProperty_thenReturnPropertyinResult() {
Assert.assertNotNull(System.getProperty("java.vm.vendor"));
}
@Test
public void givenSystem_whenCalledSetProperty_thenSetPropertyasResult() {
// set a particular property
System.setProperty("abckey", "abcvaluefoo");
Assert.assertEquals("abcvaluefoo", System.getProperty("abckey"));
}
@Test
public void givenSystem_whenCalledClearProperty_thenDeletePropertyasResult() {
// Delete a property
System.clearProperty("abckey");
Assert.assertNull(System.getProperty("abckey"));
}
@Test
public void givenSystem_whenCalledGetPropertyDefaultValue_thenReturnPropertyinResult() {
System.clearProperty("dbHost");
String myKey = System.getProperty("dbHost", "db.host.com");
Assert.assertEquals("db.host.com", myKey);
}
@Test
public void givenSystem_whenCalledGetProperties_thenReturnPropertiesinResult() {
Properties properties = System.getProperties();
Assert.assertNotNull(properties);
}
@Test
@Ignore
public void givenSystem_whenCalledClearProperties_thenDeleteAllPropertiesasResult() {
// Clears all system properties. Use with care!
System.getProperties().clear();
Assert.assertTrue(System.getProperties().isEmpty());
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.typeerasure;
import org.junit.Test;
public class TypeErasureUnitTest {
@Test(expected = ClassCastException.class)
public void givenIntegerStack_whenStringPushedAndAssignPoppedValueToInteger_shouldFail() {
IntegerStack integerStack = new IntegerStack(5);
Stack stack = integerStack;
stack.push("Hello");
Integer data = integerStack.pop();
}
@Test
public void givenAnyArray_whenInvokedPrintArray_shouldSucceed() {
Integer[] scores = new Integer[] { 100, 200, 10, 99, 20 };
ArrayContentPrintUtil.printArray(scores);
}
}