diff --git a/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsContextBuilderTest.java b/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsContextBuilderTest.java index 7a8224bb779..f947a367a83 100644 --- a/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsContextBuilderTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsContextBuilderTest.java @@ -18,14 +18,23 @@ import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException; import com.alibaba.nacos.core.remote.tls.RpcServerTlsConfig; +import com.alibaba.nacos.sys.env.EnvUtil; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.mock.env.MockEnvironment; + +import java.lang.reflect.Field; public class DefaultTlsContextBuilderTest { + private ConfigurableEnvironment environment; + @Before public void setUp() throws Exception { + environment = new MockEnvironment(); + EnvUtil.setEnvironment(environment); RpcServerTlsConfig.getInstance().setEnableTls(true); } @@ -40,6 +49,7 @@ public void tearDown() throws Exception { RpcServerTlsConfig.getInstance().setProtocols(null); RpcServerTlsConfig.getInstance().setTrustCollectionCertFile(null); RpcServerTlsConfig.getInstance().setSslProvider(""); + clearRpcServerTlsConfigInstance(); } @Test(expected = IllegalArgumentException.class) @@ -101,4 +111,10 @@ public void testGetSslContextForNonExistFile() { grpcServerConfig.setCertChainFile("non-exist-cert.pem"); DefaultTlsContextBuilder.getSslContext(RpcServerTlsConfig.getInstance()); } + + private static void clearRpcServerTlsConfigInstance() throws Exception { + Field instanceField = RpcServerTlsConfig.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + instanceField.set(null, null); + } } \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsProtocolNegotiatorBuilderTest.java b/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsProtocolNegotiatorBuilderTest.java index ac8be81b618..24e0a6fd782 100644 --- a/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsProtocolNegotiatorBuilderTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/remote/grpc/negotiator/tls/DefaultTlsProtocolNegotiatorBuilderTest.java @@ -17,19 +17,28 @@ package com.alibaba.nacos.core.remote.grpc.negotiator.tls; import com.alibaba.nacos.core.remote.tls.RpcServerTlsConfig; +import com.alibaba.nacos.sys.env.EnvUtil; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.mock.env.MockEnvironment; + +import java.lang.reflect.Field; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; public class DefaultTlsProtocolNegotiatorBuilderTest { + private ConfigurableEnvironment environment; + private DefaultTlsProtocolNegotiatorBuilder builder; @Before public void setUp() throws Exception { + environment = new MockEnvironment(); + EnvUtil.setEnvironment(environment); builder = new DefaultTlsProtocolNegotiatorBuilder(); } @@ -38,6 +47,7 @@ public void tearDown() throws Exception { RpcServerTlsConfig.getInstance().setEnableTls(false); RpcServerTlsConfig.getInstance().setCertChainFile(null); RpcServerTlsConfig.getInstance().setCertPrivateKey(null); + clearRpcServerTlsConfigInstance(); } @Test @@ -52,4 +62,10 @@ public void testBuildEnabled() { RpcServerTlsConfig.getInstance().setCertChainFile("test-server-cert.pem"); assertNotNull(builder.build()); } + + private static void clearRpcServerTlsConfigInstance() throws Exception { + Field instanceField = RpcServerTlsConfig.class.getDeclaredField("instance"); + instanceField.setAccessible(true); + instanceField.set(null, null); + } } \ No newline at end of file