small fixes

This commit is contained in:
Loredana 2019-09-08 15:52:17 +03:00
parent b6960a762f
commit 1342f75609
14 changed files with 5 additions and 265 deletions

View File

@ -8,9 +8,8 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId> <artifactId>testing-modules</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent> </parent>
<dependencies> <dependencies>
@ -47,7 +46,6 @@
<version>${jgotesting.version}</version> <version>${jgotesting.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
@ -74,8 +72,4 @@
<jgotesting.version>0.12</jgotesting.version> <jgotesting.version>0.12</jgotesting.version>
</properties> </properties>
</project> </project>

View File

@ -1,14 +0,0 @@
package com.baeldung.junit;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class AdditionUnitTest {
Calculator calculator = new Calculator();
@Test
public void testAddition() {
assertEquals("addition", 8, calculator.add(5, 3));
}
}

View File

@ -1,101 +0,0 @@
package com.baeldung.junit;
import org.junit.Test;
import java.util.Arrays;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.junit.Assert.*;
/**
* Unit test that demonstrate the different assertions available within JUnit 4
*/
public class AssertionsUnitTest {
@Test
public void whenAssertingEquality_thenEqual() {
String expected = "Baeldung";
String actual = "Baeldung";
assertEquals(expected, actual);
}
@Test
public void whenAssertingEqualityWithMessage_thenEqual() {
String expected = "Baeldung";
String actual = "Baeldung";
assertEquals("failure - strings are not equal", expected, actual);
}
@Test
public void whenAssertingArraysEquality_thenEqual() {
char[] expected = { 'J', 'u', 'n', 'i', 't' };
char[] actual = "Junit".toCharArray();
assertArrayEquals(expected, actual);
}
@Test
public void givenNullArrays_whenAssertingArraysEquality_thenEqual() {
int[] expected = null;
int[] actual = null;
assertArrayEquals(expected, actual);
}
@Test
public void whenAssertingNull_thenTrue() {
Object car = null;
assertNull("The car should be null", car);
}
@Test
public void whenAssertingNotNull_thenTrue() {
Object car = new Object();
assertNotNull("The car should not be null", car);
}
@Test
public void whenAssertingNotSameObject_thenDifferent() {
Object cat = new Object();
Object dog = new Object();
assertNotSame(cat, dog);
}
@Test
public void whenAssertingSameObject_thenSame() {
Object cat = new Object();
assertSame(cat, cat);
}
@Test
public void whenAssertingConditions_thenVerified() {
assertTrue("5 is greater then 4", 5 > 4);
assertFalse("5 is not greater then 6", 5 > 6);
}
@Test
public void when_thenNotFailed() {
try {
methodThatShouldThrowException();
fail("Exception not thrown");
} catch (UnsupportedOperationException e) {
assertEquals("Operation Not Supported", e.getMessage());
}
}
private void methodThatShouldThrowException() {
throw new UnsupportedOperationException("Operation Not Supported");
}
@Test
public void testAssertThatHasItems() {
assertThat(Arrays.asList("Java", "Kotlin", "Scala"), hasItems("Java", "Kotlin"));
}
}

View File

@ -1,18 +0,0 @@
package com.baeldung.junit;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
public class BlockingTestRunner extends BlockJUnit4ClassRunner {
public BlockingTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
System.out.println("invoking: " + method.getName());
return super.methodInvoker(method, test);
}
}

View File

@ -1,17 +0,0 @@
package com.baeldung.junit;
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 CalculatorUnitTest {
Calculator calculator = new Calculator();
@Test
public void testAddition() {
assertEquals("addition", 8, calculator.add(5, 3));
}
}

View File

@ -1,14 +0,0 @@
package com.baeldung.junit;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SubstractionUnitTest {
Calculator calculator = new Calculator();
@Test
public void substraction() {
assertEquals("substraction", 2, calculator.sub(5, 3));
}
}

View File

@ -1,12 +0,0 @@
package com.baeldung.junit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
AdditionUnitTest.class,
SubstractionUnitTest.class})
public class SuiteUnitTest {
}

View File

@ -1,41 +0,0 @@
package com.baeldung.junit;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import java.lang.reflect.Method;
public class TestRunner extends Runner {
private Class testClass;
public TestRunner(Class testClass) {
super();
this.testClass = testClass;
}
@Override
public Description getDescription() {
return Description.createTestDescription(testClass, "My runner description");
}
@Override
public void run(RunNotifier notifier) {
System.out.println("running the tests from MyRunner: " + testClass);
try {
Object testObject = testClass.newInstance();
for (Method method : testClass.getMethods()) {
if (method.isAnnotationPresent(Test.class)) {
notifier.fireTestStarted(Description
.createTestDescription(testClass, method.getName()));
method.invoke(testObject);
notifier.fireTestFinished(Description
.createTestDescription(testClass, method.getName()));
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -3,7 +3,6 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>mocks</artifactId> <artifactId>mocks</artifactId>
<name>mocks</name> <name>mocks</name>
<packaging>pom</packaging>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>

View File

@ -28,17 +28,4 @@ public class AppManagerUnitTest {
Assertions.assertFalse(appManager.managerResponse("Why are you coming late?")); Assertions.assertFalse(appManager.managerResponse("Why are you coming late?"));
} }
@Test
public void givenAppManager_whenPrivateStaticMethod_thenValidateExpectedResponse() {
final int response = Deencapsulation.invoke(AppManager.class, "stringToInteger", "110");
Assertions.assertEquals(110, response);
}
@Test
public void givenAppManager_whenPrivateStaticMethod_thenExpectException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
Deencapsulation.invoke(AppManager.class, "stringToInteger", "11r");
});
}
} }

View File

@ -1,12 +1,10 @@
package org.baeldung.mocks.jmockit; package org.baeldung.mocks.jmockit;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.baeldung.mocks.jmockit.AdvancedCollaborator.InnerAdvancedCollaborator;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -52,12 +50,6 @@ public class AdvancedCollaboratorIntegrationTest<MultiMock extends List<String>
assertEquals(1, coll.i); assertEquals(1, coll.i);
} }
@Test
public void testToCallPrivateMethodsDirectly() {
Object value = Deencapsulation.invoke(mock, "privateMethod");
assertEquals("default:", value);
}
@Test @Test
public void testToSetPrivateFieldDirectly() { public void testToSetPrivateFieldDirectly() {
Deencapsulation.setField(mock, "privateField", 10); Deencapsulation.setField(mock, "privateField", 10);
@ -70,18 +62,6 @@ public class AdvancedCollaboratorIntegrationTest<MultiMock extends List<String>
assertEquals(5, value); assertEquals(5, value);
} }
@Test
public void testToCreateNewInstanceDirectly() {
AdvancedCollaborator coll = Deencapsulation.newInstance(AdvancedCollaborator.class, "foo");
assertEquals(3, coll.i);
}
@Test
public void testToCreateNewInnerClassInstanceDirectly() {
InnerAdvancedCollaborator innerCollaborator = Deencapsulation.newInnerInstance(InnerAdvancedCollaborator.class, mock);
assertNotNull(innerCollaborator);
}
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testMultipleInterfacesWholeTest() { public void testMultipleInterfacesWholeTest() {

View File

@ -3,7 +3,6 @@ package org.baeldung.mocks.jmockit;
import mockit.Delegate; import mockit.Delegate;
import mockit.Expectations; import mockit.Expectations;
import mockit.Mocked; import mockit.Mocked;
import mockit.StrictExpectations;
import mockit.Verifications; import mockit.Verifications;
import mockit.integration.junit4.JMockit; import mockit.integration.junit4.JMockit;
import org.hamcrest.BaseMatcher; import org.hamcrest.BaseMatcher;
@ -106,7 +105,7 @@ public class ExpectationsIntegrationTest {
@Test @Test
public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) { public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) {
new StrictExpectations() {{ new Expectations() {{
mock.methodReturnsString(); mock.methodReturnsString();
result = "foo"; result = "foo";
result = new Exception(); result = new Exception();

View File

@ -4,13 +4,11 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>testing-libraries</artifactId> <artifactId>testing-libraries</artifactId>
<name>testing-libraries</name> <name>testing-libraries</name>
<packaging>pom</packaging>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId> <artifactId>testing-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent> </parent>
<dependencies> <dependencies>

View File

@ -1,4 +1,4 @@
package com.baeldung.testing.mutation; package com.baeldung.mutation;
public class Palindrome { public class Palindrome {