SCB-1400 Complement and improve unit test
fix code according code review
Signed-off-by: MabinGo <bin.ma@huawei.com>
diff --git a/common/src/test/java/org/apache/servicecomb/toolkit/common/FileUtilsTest.java b/common/src/test/java/org/apache/servicecomb/toolkit/common/FileUtilsTest.java
index 56a9bd5..e902662 100755
--- a/common/src/test/java/org/apache/servicecomb/toolkit/common/FileUtilsTest.java
+++ b/common/src/test/java/org/apache/servicecomb/toolkit/common/FileUtilsTest.java
@@ -20,7 +20,6 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
@@ -38,39 +37,32 @@
path = Files.createTempDirectory("");
FileUtils.createDirectory(path.toFile().getCanonicalPath());
} catch (IOException e) {
- fail("Run 'createDirectoryTest' failed and unexpected to catch IOException: " + e.getMessage());
+ fail("Run 'createDirectoryTest' failed, unexpected to catch IOException: " + e.getMessage());
}
try {
FileUtils.createDirectory(null);
+ fail("Run 'createDirectoryTest' failed, expected to occur IOException but not");
} catch (IOException e) {
assertEquals("Path is null", e.getMessage());
- return;
}
-
- fail("Run 'createDirectoryTest' failed, expected to catch IOException but not");
}
@Test
public void getFilesGroupByFilenameTest() {
- boolean succeed = false;
try {
- succeed = false;
FileUtils.getFilesGroupByFilename(null);
+ fail("Run 'getFilesGroupByFilenameTest' failed, expected to occur IOException but not");
} catch (IOException e) {
assertEquals("Path is null", e.getMessage());
- succeed = true;
}
- assertTrue(succeed);
try {
FileUtils.getFilesGroupByFilename("");
+ fail("Run 'getFilesGroupByFilenameTest' failed, expected to occur IOException but not");
} catch (IOException e) {
assertThat(e.getMessage(), containsString("is not exists"));
- return;
}
-
- fail("Run 'getFilesGroupByFilenameTest' failed, expected to catch IOException but not");
}
}
\ No newline at end of file
diff --git a/contractgen/src/test/java/org/apache/servicecomb/toolkit/contractgen/DefaultContractsGeneratorTest.java b/contractgen/src/test/java/org/apache/servicecomb/toolkit/contractgen/DefaultContractsGeneratorTest.java
index f282851..f9f5ac7 100644
--- a/contractgen/src/test/java/org/apache/servicecomb/toolkit/contractgen/DefaultContractsGeneratorTest.java
+++ b/contractgen/src/test/java/org/apache/servicecomb/toolkit/contractgen/DefaultContractsGeneratorTest.java
@@ -17,9 +17,11 @@
package org.apache.servicecomb.toolkit.contractgen;
+import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
@@ -107,51 +109,51 @@
method.setAccessible(true);
defaultContractsGenerator.configure(null);
- assertFalse((boolean) method.invoke(defaultContractsGenerator, new Object[] {}));
+ assertThat(method.invoke(defaultContractsGenerator), is(false));
Map<String, Object> config = new HashMap<>();
config.put("classpathUrls", null);
defaultContractsGenerator.configure(config);
- assertFalse((boolean) method.invoke(defaultContractsGenerator, new Object[] {}));
+ assertThat(method.invoke(defaultContractsGenerator), is(false));
MavenProject project = new MavenProject();
config.put("classpathUrls", project.getRuntimeClasspathElements());
defaultContractsGenerator.configure(config);
- assertTrue((boolean) method.invoke(defaultContractsGenerator, new Object[] {}));
+ assertThat(method.invoke(defaultContractsGenerator), is(true));
}
@Test
public void testPrivateCanProcess() throws Exception {
DefaultContractsGenerator defaultContractsGenerator = new DefaultContractsGenerator();
- Method method = defaultContractsGenerator.getClass().getDeclaredMethod("canProcess", new Class[] {Class.class});
+ Method method = defaultContractsGenerator.getClass().getDeclaredMethod("canProcess", Class.class);
method.setAccessible(true);
- assertFalse((boolean) method.invoke(defaultContractsGenerator, new Object[] {null}));
+ assertThat(method.invoke(defaultContractsGenerator, new Object[] {null}), is(false));
Class mockClass;
ContractTestUtil contractTestUtil = new ContractTestUtil();
CtClass ctClass = contractTestUtil.createCtClass("TestCanProcess");
- assertFalse((boolean) method.invoke("TestCanProcess", ctClass.toClass()));
+ assertThat(method.invoke("TestCanProcess", ctClass.toClass()), is(false));
mockClass = contractTestUtil.putAnnotationToClass("TestRestSchema", RestSchema.class);
- assertTrue((boolean) method.invoke("TestRestSchema", mockClass));
+ assertThat(method.invoke("TestRestSchema", mockClass), is(true));
mockClass = contractTestUtil.putAnnotationToClass("TestRestController", RestController.class);
- assertTrue((boolean) method.invoke("TestRestController", mockClass));
+ assertThat(method.invoke("TestRestController", mockClass), is(true));
mockClass = contractTestUtil.putAnnotationToClass("TestRpcSchema", RpcSchema.class);
- assertTrue((boolean) method.invoke("TestRpcSchema", mockClass));
+ assertThat(method.invoke("TestRpcSchema", mockClass), is(true));
mockClass = contractTestUtil.putAnnotationToClass("TestRequestMapping", RequestMapping.class);
- assertTrue((boolean) method.invoke("TestRequestMapping", mockClass));
+ assertThat(method.invoke("TestRequestMapping", mockClass), is(true));
}
@Test
public void testgetAllClass() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
DefaultContractsGenerator defaultContractsGenerator = new DefaultContractsGenerator();
Method method = defaultContractsGenerator.getClass()
- .getDeclaredMethod("getAllClass", new Class[] {ClassLoader.class});
+ .getDeclaredMethod("getAllClass", ClassLoader.class);
method.setAccessible(true);
try {
diff --git a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java
index 3f2d6df..d34a336 100755
--- a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java
+++ b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateMojoTest.java
@@ -21,7 +21,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -59,8 +58,6 @@
String projectOutput = null;
String documentOutput = null;
- boolean succeed;
-
final MavenProject project = mock(MavenProject.class);
// code has no contract
@@ -78,7 +75,7 @@
assertEquals(0, Objects
.requireNonNull(new File(testResourcesEx.getVariableValueFromObject("contractLocation")).listFiles()).length);
} catch (MojoFailureException e) {
- fail("Run 'testGenerateMojo' failed and unexpected to catch MojoFailureException: " + e.getMessage());
+ fail("Run 'testGenerateMojo' failed, unexpected to catch MojoFailureException: " + e.getMessage());
}
// code has contract
@@ -103,34 +100,30 @@
assertNotEquals(0, Objects.requireNonNull(new File(projectOutput).listFiles()).length);
assertNotEquals(0, Objects.requireNonNull(new File(documentOutput).listFiles()).length);
} catch (RuntimeException e) {
- fail("Run 'testGenerateMojo' failed and unexpected to catch RuntimeException: " + e.getMessage());
+ fail("Run 'testGenerateMojo' failed, unexpected to catch RuntimeException: " + e.getMessage());
}
try {
- succeed = false;
-
testResourcesEx.setVariableValueToObject("sourceType", "contract");
testResourcesEx.setVariableValueToObject("contractLocation", null);
testResourcesEx.execute();
+
+ fail("Run 'testGenerateMojo' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Invalid or not config contract location", e.getMessage());
- succeed = true;
}
- assertTrue(succeed);
try {
- succeed = false;
-
testResourcesEx.setVariableValueToObject("sourceType", "contract");
testResourcesEx.setVariableValueToObject("contractLocation", "");
testResourcesEx.execute();
+
+ fail("Run 'testGenerateMojo' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertThat(e.getMessage(), containsString("is not exists"));
- succeed = true;
}
- assertTrue(succeed);
try {
outputDirectory = "target";
@@ -147,7 +140,7 @@
assertNotEquals(0, Objects.requireNonNull(new File(projectOutput).listFiles()).length);
assertNotEquals(0, Objects.requireNonNull(new File(documentOutput).listFiles()).length);
} catch (RuntimeException e) {
- fail("Run 'testGenerateMojo' failed and unexpected to catch RuntimeException: " + e.getMessage());
+ fail("Run 'testGenerateMojo' failed, unexpected to catch RuntimeException: " + e.getMessage());
}
outputDirectory = "target";
diff --git a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java
index e485f70..edebece 100755
--- a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java
+++ b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/GenerateUtilTest.java
@@ -58,12 +58,11 @@
when(project.getRuntimeClasspathElements()).thenThrow(DependencyResolutionRequiredException.class);
try {
generateContract(project, contractOutput, "yaml", "default");
+
+ fail("Run 'testGenerateContract' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to get runtime class elements", e.getMessage());
- return;
}
-
- fail("Run 'testGenerateContract' failed, expected to catch RuntimeException but not");
}
@Test
@@ -78,12 +77,11 @@
try {
generateCode(service, contractLocation, projectOutput, "invalidType");
+
+ fail("Run 'testGenerateCode' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Cannot found code generator's implementation", e.getMessage());
- return;
}
-
- fail("Run 'testGenerateCode' failed, expected to catch RuntimeException but not");
}
@Test
@@ -97,11 +95,10 @@
try {
generateDocument(contractLocation, codeOutput, "invalidType");
+
+ fail("Run 'testGenerateDocument' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Cannot found document generator's implementation", e.getMessage());
- return;
}
-
- fail("Run 'testGenerateDocument' failed, expected to catch RuntimeException but not");
}
}
\ No newline at end of file
diff --git a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java
index 3ca5723..053e33a 100755
--- a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java
+++ b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/InvokeStaticMethodTest.java
@@ -89,13 +89,12 @@
testVerifyMojoResources.setVariableValueToObject("sourceType", "code");
try {
testVerifyMojoResources.execute();
+
+ fail("Run 'testVerifyMojoInvokeGenerateUtilGenerateContract' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate contract from code", e.getMessage());
assertThat(e.getCause().toString(), containsString("RuntimeException"));
- return;
}
-
- fail("Run 'testVerifyMojoInvokeGenerateUtilGenerateContract' failed, expected to catch RuntimeException but not");
}
@Test
@@ -110,13 +109,12 @@
testVerifyMojoResources.setVariableValueToObject("sourceType", "code");
try {
testVerifyMojoResources.execute();
+
+ fail("Run 'testVerifyMojoInvokeFileUtilsCreateTempDirectory' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate contract from code", e.getMessage());
assertThat(e.getCause().toString(), containsString("IOException"));
- return;
}
-
- fail("Run 'testVerifyMojoInvokeFileUtilsCreateTempDirectory' failed, expected to catch RuntimeException but not");
}
@Test
@@ -131,21 +129,18 @@
testGenerateMojoResources.setVariableValueToObject("sourceType", "code");
try {
testGenerateMojoResources.execute();
+
+ fail("Run 'testGenerateMojoInvokeFileUtilsCreateDirectory' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate contract", e.getMessage());
assertThat(e.getCause().toString(), containsString("IOException"));
- return;
}
-
- fail("Run 'testGenerateMojoInvokeFileUtilsCreateDirectory' failed, expected to catch RuntimeException but not");
}
@Test
public void testGenerateMojoInvokeGenerateUtilGenerateCode()
throws IllegalAccessException, MojoFailureException, MojoExecutionException, IOException {
- boolean succeed = false;
-
PowerMockito.mockStatic(GenerateUtil.class);
PowerMockito.doThrow(new IOException()).when(GenerateUtil.class);
// Powermockito limit: use argument matchers to specify method which would be mock
@@ -157,32 +152,29 @@
testGenerateMojoResources.setVariableValueToObject("service", new ServiceConfig());
try {
testGenerateMojoResources.execute();
+
+ fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateCode' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate code", e.getMessage());
assertThat(e.getCause().toString(), containsString("IOException"));
- succeed = true;
}
- assertTrue(succeed);
PowerMockito.doThrow(new RuntimeException()).when(GenerateUtil.class);
GenerateUtil.generateCode(anyObject(), anyString(), anyString(), anyString());
try {
testGenerateMojoResources.execute();
+
+ fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateCode' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate code", e.getMessage());
assertThat(e.getCause().toString(), containsString("RuntimeException"));
- return;
}
-
- fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateCode' failed, expected to catch RuntimeException but not");
}
@Test
public void testGenerateMojoInvokeGenerateUtilGenerateDocument()
throws IllegalAccessException, MojoFailureException, MojoExecutionException, IOException {
- boolean succeed = false;
-
PowerMockito.mockStatic(GenerateUtil.class);
PowerMockito.doThrow(new IOException()).when(GenerateUtil.class);
// Powermockito limit: use argument matchers to specify method which would be mock
@@ -194,23 +186,22 @@
testGenerateMojoResources.setVariableValueToObject("service", new ServiceConfig());
try {
testGenerateMojoResources.execute();
+
+ fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateDocument' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate document", e.getMessage());
assertThat(e.getCause().toString(), containsString("IOException"));
- succeed = true;
}
- assertTrue(succeed);
PowerMockito.doThrow(new RuntimeException()).when(GenerateUtil.class);
GenerateUtil.generateDocument(anyString(), anyString(), anyString());
try {
testGenerateMojoResources.execute();
+
+ fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateDocument' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertEquals("Failed to generate document", e.getMessage());
assertThat(e.getCause().toString(), containsString("RuntimeException"));
- return;
}
-
- fail("Run 'testGenerateMojoInvokeGenerateUtilGenerateDocument' failed, expected to catch RuntimeException but not");
}
}
diff --git a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java
index 45e6dd7..d604d91 100644
--- a/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java
+++ b/toolkit-maven-plugin/src/test/java/org/apache/servicecomb/toolkit/plugin/VerifyMojoTest.java
@@ -19,7 +19,6 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -38,8 +37,6 @@
@Rule
public TestResources resources = new TestResources();
- boolean succeed;
-
@Test
public void testVerifyMojo() throws Exception {
@@ -58,31 +55,31 @@
testResourcesEx.setVariableValueToObject("destinationContractPath", testResourcesEx.getContractDestination());
testResourcesEx.execute();
} catch (RuntimeException e) {
- fail("Run 'testVerifyMojo' failed and unexpected to catch RuntimeException: " + e.getMessage());
+ fail("Run 'testVerifyMojo' failed, unexpected to catch RuntimeException: " + e.getMessage());
}
try {
- succeed = false;
testResourcesEx.setVariableValueToObject("sourceType", "contract");
testResourcesEx.setVariableValueToObject("sourceContractPath", null);
+
testResourcesEx.execute();
+
+ fail("Run 'testVerifyMojo' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertThat(e.getMessage(), containsString("Failed to verify contract"));
- succeed = true;
}
- assertTrue(succeed);
try {
- succeed = false;
testResourcesEx.setVariableValueToObject("sourceType", "contract");
testResourcesEx.setVariableValueToObject("sourceContractPath", testResourcesEx.getContractLocation());
testResourcesEx.setVariableValueToObject("destinationContractPath", null);
+
testResourcesEx.execute();
+
+ fail("Run 'testVerifyMojo' failed, expected to occur RuntimeException but not");
} catch (RuntimeException e) {
assertThat(e.getMessage(), containsString("Failed to verify contract"));
- succeed = true;
}
- assertTrue(succeed);
try {
testResourcesEx.setVariableValueToObject("sourceType", "contract");
@@ -90,7 +87,7 @@
testResourcesEx.setVariableValueToObject("destinationContractPath", testResourcesEx.getContractDestination());
testResourcesEx.execute();
} catch (RuntimeException e) {
- fail("Run 'testVerifyMojo' failed and unexpected to catch RuntimeException: " + e.getMessage());
+ fail("Run 'testVerifyMojo' failed, unexpected to catch RuntimeException: " + e.getMessage());
}
}
}
\ No newline at end of file