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

Test: add new test cases. #1299

Merged
merged 9 commits into from
Mar 22, 2024
6 changes: 6 additions & 0 deletions sofa-boot-project/sofa-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add this dependency?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ci run fail, please check it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • removed the dependency it was a mistake.
  • PMD fails are resolved pipeline should not break now.

Copy link
Contributor

@HzjNeverStop HzjNeverStop Mar 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please run “mvn compile” to update code format before you submit pr

<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alipay.sofa.boot.compatibility;

import com.alipay.sofa.boot.compatibility.VerificationResult;
import com.alipay.sofa.boot.util.SofaBootEnvUtils;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;

/**
* Tests for {@link VerificationResult}.
*
* @author JPSINH27
* @version VerificationResultTests, v 0.1 2024年03月02日 10:20 PM
*/
public class VerificationResultTests {

@Test
public void testEquals_SameDescriptionAndAction_ReturnsTrue() {
VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action");
VerificationResult result2 = VerificationResult.notCompatible("Error", "Take action");
assertThat(result1).isEqualTo(result2);
}

@Test
public void testEquals_DifferentDescriptions_ReturnsFalse() {
VerificationResult result1 = VerificationResult.notCompatible("Error 1", "Take action");
VerificationResult result2 = VerificationResult.notCompatible("Error 2", "Take action");
assertThat(result1).isNotEqualTo(result2);
}

@Test
public void testEquals_DifferentActions_ReturnsFalse() {
VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action 1");
VerificationResult result2 = VerificationResult.notCompatible("Error", "Take action 2");
assertThat(result1).isNotEqualTo(result2);
}

@Test
public void testEquals_ComparingWithNull_ReturnsFalse() {
VerificationResult result1 = VerificationResult.notCompatible("Error", "Take action");
assertThat(result1).isNotEqualTo(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public void afterRefresh(SofaGenericApplicationContext context, Throwable throwa
failed = throwable != null;
}



public boolean isStarted() {
return started;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.alipay.sofa.boot.startup;

import com.alipay.sofa.boot.env.SofaBootEnvironmentPostProcessor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Tests for {@link StartupReporter}.
*
* @author JPSINH27
* @version StartupReporterTests.java, v 0.1 2024年01月03日 10:19 PM
*/
public class StartupReporterTests {

@Mock
ConfigurableApplicationContext mockContext;

@Mock
ConfigurableEnvironment mockEnvironment;

@BeforeEach
public void setup() {
MockitoAnnotations.openMocks(this);
}
@Test
public void testApplicationBootFinish() {
StartupReporter startupReporter = new StartupReporter();
assertDoesNotThrow(startupReporter::applicationBootFinish);
}

@Test
public void testAddCommonStartupStat() {
StartupReporter startupReporter = new StartupReporter();
BaseStat baseStat = new BaseStat();
assertDoesNotThrow(() -> {
startupReporter.addCommonStartupStat(baseStat);
});
}


@Test
public void testDrainStartupStaticsModel() {
StartupReporter startupReporter = new StartupReporter();
assertNotNull(startupReporter.drainStartupStaticsModel());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
package com.alipay.sofa.boot.util;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.cloud.util.PropertyUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

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

/**
* Tests for {@link SofaBootEnvUtils}.
Expand Down Expand Up @@ -49,4 +56,43 @@ public void testEnv() {
public void arkEnv() {
assertThat(SofaBootEnvUtils.isArkEnv()).isFalse();
}

@Test
void testIsSpringCloudBootstrapEnvironment_NullEnvironment() {
assertFalse(SofaBootEnvUtils.isSpringCloudBootstrapEnvironment(null));
}

@Test
public void testInitSpringTestEnv() {

boolean expectedTestEnv = true;

boolean actualTestEnv = isInitSpringTestEnv();

assertEquals(expectedTestEnv, actualTestEnv);
}

private boolean isInitSpringTestEnv() {
StackTraceElement[] stackTrace = new StackTraceElement[]{
new StackTraceElement("SomeClass", "someMethod", "SomeClass.java", 10),
new StackTraceElement("AnotherClass", "loadContext", "AnotherClass.java", 20),
new StackTraceElement("org.springframework.boot.test.context.SpringBootContextLoader",
"loadContext", "SpringBootContextLoader.java", 30)
};
boolean TEST_ENV = false;
for (StackTraceElement stackTraceElement : stackTrace) {
if ("loadContext".equals(stackTraceElement.getMethodName())
&& "org.springframework.boot.test.context.SpringBootContextLoader"
.equals(stackTraceElement.getClassName())) {
TEST_ENV = true;
break;
}
}
return TEST_ENV;
}
}





Loading