Skip to content

Commit

Permalink
refactor (jkube-kit/common) : Migrate all tests from JMockit to Mocki…
Browse files Browse the repository at this point in the history
…to (#1545) (#1609)

* refactor (jkube-kit/common) : Migrate all tests from JMockit to Mockito (#1545)

* Update JKubeProjectUtilTest.java

Update JKubeProjectUtilTest.java

PR Review changes

* All tests from JMockit to Mockito

Update JKubeProjectUtilTest.java

PR Review changes

refactor (jkube-kit/common) : Migrate all tests from JMockit to Mockito (#1545)

* PR Review Changes

Update KubernetesHelperTest.java

* review: JMockit to Mockito

Signed-off-by: Marc Nuri <marc@marcnuri.com>

Co-authored-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
baruKreddy and manusa committed Jul 5, 2022
1 parent 6134582 commit 698fb4c
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 193 deletions.
4 changes: 2 additions & 2 deletions jkube-kit/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,37 +114,52 @@ public void getStringValueTest() {
@Test
public void getFromSystemPropertyWithPropertiesAsFallbackHasKeyInSystemShouldReturnSystemValue() {
// Given
new SystemMock().put("key", "systemValue");
final Properties fallback = new Properties();
fallback.put("key", "fallbackValue");
// When
final String result = Configs.getFromSystemPropertyWithPropertiesAsFallback(fallback, "key");
// Then
assertThat(result).isEqualTo("systemValue");
try {
System.setProperty("key", "systemValue");
// TODO : Replace this when https://github.com/eclipse/jkube/issues/958 gets fixed
final Properties fallback = new Properties();
fallback.put("key", "fallbackValue");
// When
final String result = Configs.getFromSystemPropertyWithPropertiesAsFallback(fallback, "key");
// Then
assertThat(result).isEqualTo("systemValue");
} finally {
System.clearProperty("key");
}
}

@Test
public void getFromSystemPropertyWithPropertiesAsFallbackHasNotKeyInSystemShouldReturnSystemValue() {
// Given
new SystemMock().put("not-the-key", "systemValue");
final Properties fallback = new Properties();
fallback.put("key", "fallbackValue");
// When
final String result = Configs.getFromSystemPropertyWithPropertiesAsFallback(fallback, "key");
// Then
assertThat(result).isEqualTo("fallbackValue");
try {
System.setProperty("not-the-key", "systemValue");
// TODO : Replace this when https://github.com/eclipse/jkube/issues/958 gets fixed
final Properties fallback = new Properties();
fallback.put("key", "fallbackValue");
// When
final String result = Configs.getFromSystemPropertyWithPropertiesAsFallback(fallback, "key");
// Then
assertThat(result).isEqualTo("fallbackValue");
} finally {
System.clearProperty("not-the-key");
}
}

@Test
public void getFromSystemPropertyWithPropertiesAsFallbackHasNotKeyShouldReturnNull() {
// Given
new SystemMock().put("not-the-key", "systemValue");
final Properties fallback = new Properties();
fallback.put("not-the-key-either", "fallbackValue");
// When
final String result = Configs.getFromSystemPropertyWithPropertiesAsFallback(fallback, "key");
// Then
assertThat(result).isNull();
try {
System.setProperty("not-the-key", "systemValue");
// TODO : Replace this when https://github.com/eclipse/jkube/issues/958 gets fixed
final Properties fallback = new Properties();
fallback.put("not-the-key-either", "fallbackValue");
// When
final String result = Configs.getFromSystemPropertyWithPropertiesAsFallback(fallback, "key");
// Then
assertThat(result).isNull();
} finally {
System.clearProperty("not-the-key");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@

import org.eclipse.jkube.kit.common.KitLogger;

import mockit.Mocked;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@RunWith(MockitoJUnitRunner.class)
public class MigrateServiceTest {
@Mocked
@Mock
KitLogger logger;

@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,10 @@
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import org.eclipse.jkube.kit.common.SystemMock;

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.eclipse.jkube.kit.common.util.EnvUtil.firstRegistryOf;
Expand Down Expand Up @@ -175,7 +171,7 @@ public void testSplitOnLastColonWhenNull() {
}

@Test
public void testRemoveEmptyEntrieWhenNotNull(){
public void testRemoveEmptyEntriesWhenNotNull(){
//Given
List<String> string1 = new ArrayList<>();
string1.add(" set ");
Expand Down Expand Up @@ -396,31 +392,46 @@ public void testLoadTimestampShouldLoadFromFile() throws Exception {

@Test
public void testIsWindowsFalse(){
//Given
new SystemMock().put("os.name", "random");
//When
boolean result= EnvUtil.isWindows();
//Then
assertThat(result).isFalse();
String oldOsName = System.getProperty("os.name");
try {
//Given
System.setProperty("os.name", "random");
// TODO : Replace this when https://github.com/eclipse/jkube/issues/958 gets fixed
//When
boolean result = EnvUtil.isWindows();
//Then
assertThat(result).isFalse();
} finally {
System.setProperty("os.name", oldOsName);
}
}

@Test
public void testIsWindows(){
String oldOsName = System.getProperty("os.name");
try {
//Given
new SystemMock().put("os.name", "windows");
System.setProperty("os.name", "windows");
// TODO : Replace this when https://github.com/eclipse/jkube/issues/958 gets fixed
//When
boolean result= EnvUtil.isWindows();
boolean result = EnvUtil.isWindows();
//Then
assertThat(result).isTrue();
} finally {
System.setProperty("os.name", oldOsName);
}
}

@Test
public void testSystemPropertyRead() {
System.setProperty("testProperty", "testPropertyValue");
String propertyValue =
EnvUtil.getEnvVarOrSystemProperty("testProperty", "defaultValue");
assertThat(propertyValue).isEqualTo("testPropertyValue");
System.clearProperty("testProperty");
try {
String propertyValue =
EnvUtil.getEnvVarOrSystemProperty("testProperty", "defaultValue");
assertThat(propertyValue).isEqualTo("testPropertyValue");
} finally {
System.clearProperty("testProperty");
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@
*/
package org.eclipse.jkube.kit.common.util;


import mockit.Expectations;
import mockit.Mocked;
import org.eclipse.jkube.kit.common.Dependency;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.SystemMock;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -30,8 +26,9 @@
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SuppressWarnings("ResultOfMethodCallIgnored")
public class JKubeProjectUtilTest {

@Rule
Expand All @@ -46,35 +43,29 @@ public void hasDependencyWithGroupIdWithNulls() {
}

@Test
public void hasDependencyWithGroupIdWithDependency(@Mocked JavaProject project) {
public void hasDependencyWithGroupIdWithDependency() {
// Given
// @formatter:off
new Expectations() {{
project.getDependencies(); result = Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
);
}};
// @formatter:on
final JavaProject project = mock(JavaProject.class);
when(project.getDependencies()).thenReturn(Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
));
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(project, "io.dep");
// Then
assertThat(result).isTrue();
}

@Test
public void hasDependencyWithGroupIdWithNoDependency(@Mocked JavaProject project) {
public void hasDependencyWithGroupIdWithNoDependency() {
// Given
// @formatter:off
new Expectations() {{
project.getDependencies(); result = Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
);
}};
// @formatter:on
final JavaProject project = mock(JavaProject.class);
when(project.getDependencies()).thenReturn(Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
));
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(project, "io.nothere");
// Then
Expand Down Expand Up @@ -177,14 +168,19 @@ public void getFinalOutputArtifact_withArtifactAndBuildFinalNameAndPackaging_ret
@Test
public void getProperty_whenSystemPropertyPresent_returnsSystemProperty() {
// Given
new SystemMock().put("jkube.testProperty", "true");
JavaProject javaProject = JavaProject.builder().build();

// When
String result = JKubeProjectUtil.getProperty("jkube.testProperty", javaProject);

// Then
assertThat(result).isEqualTo("true");
try {
System.setProperty("jkube.testProperty", "true");
// TODO : Replace this when https://github.com/eclipse/jkube/issues/958 gets fixed
JavaProject javaProject = JavaProject.builder().build();

// When
String result = JKubeProjectUtil.getProperty("jkube.testProperty", javaProject);

// Then
assertThat(result).isEqualTo("true");
} finally {
System.clearProperty("jkube.testProperty");
}
}

@Test
Expand Down Expand Up @@ -234,4 +230,4 @@ public void resolveArtifact_whenNoArtifactPresent_shouldThrowException() {
// Then
assertThat(illegalStateException).hasMessage("Cannot find artifact test-artifact-0.0.1.jar within the resolved resources");
}
}
}
Loading

0 comments on commit 698fb4c

Please sign in to comment.