Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test part 1 #242

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,37 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--先屏蔽javadoc的严谨告警,后续在优化 -->
<additionalparam>-Xdoclint:none</additionalparam>
<plugin.surefire.version>3.0.0-M5</plugin.surefire.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<build>
<plugins>
Expand Down Expand Up @@ -121,6 +147,41 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<!-- <plugin>-->
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
<!-- <version>${plugin.surefire.version}</version>-->
<!-- <configuration>-->
<!-- <argLine>&#45;&#45;add-opens java.base/java.lang=ALL-UNNAMED</argLine>-->
<!-- </configuration>-->
<!-- </plugin>-->
</plugins>
</build>
<licenses>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.tencentcloudapi.unit.common;

import com.tencentcloudapi.common.AbstractClient;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;


public class AbstractClientInitTest {
private Credential credential;
private ClientProfile clientProfile;

@Before
public void setUp() {
credential =
new Credential(
System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"));
clientProfile = new ClientProfile();

HttpProfile httpProfile = new HttpProfile();
clientProfile.setHttpProfile(httpProfile);
clientProfile.setSignMethod("HmacSHA256");
}

@Test
public void testAbstractClientWithEndpointVersionCredentialRegion() {
String endpoint = "example.com";
String version = "v1";
String region = "ap-guangzhou";
AbstractClient client = new AbstractClient(endpoint, version, credential, region) {
};

client.setRegion(region);
client.setClientProfile(clientProfile);
client.setCredential(credential);
assertEquals(region, client.getRegion());
assertNotNull(client.getCredential());
assertNotNull(client.getClientProfile());

}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.tencentcloudapi.unit.common;

import com.tencentcloudapi.common.AbstractClient;
import com.tencentcloudapi.common.AbstractModel;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import org.junit.Before;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.reflect.Whitebox;

import java.util.HashMap;

class Client extends AbstractClient {
public Client(String endpoint, String version, Credential credential, String region) {
super(endpoint, version, credential, region);
}

public Client(String endpoint, String version, Credential credential, String region, ClientProfile profile) {
super(endpoint, version, credential, region, profile);
}
}

public class AbstractClientInternalRequestTest {
private Credential credential;
private ClientProfile clientProfile;
private String endpoint = "example.com";
private String version = "v1";
private String region = "ap-guangzhou";

@Before
public void setUp() {
credential =
new Credential(
System.getenv("TENCENTCLOUD_SECRET_ID"), System.getenv("TENCENTCLOUD_SECRET_KEY"), "123e4567-e89b-12d3-a456-426614174000");
clientProfile = new ClientProfile();
}

@Test
public void testAbstractInternalRequest() throws Exception {
clientProfile.setBackupEndpoint("example.com");
Client client = new Client(endpoint, version, credential, region, clientProfile);
AbstractModel req = new AbstractModel() {
@Override
protected void toMap(HashMap<String, String> map, String prefix) {
}
};
String actionName = "ActionName";

Client client1 = PowerMockito.spy(client);
PowerMockito.doReturn(null).when(client1, "internalRequestRaw", req, actionName);
try {
Whitebox.invokeMethod(client1, "internalRequest", req, actionName);
} catch (Exception e) {
System.out.println(e);
}
}
}

Loading