Skip to content

Commit

Permalink
Migrates config-client tests to JUnit5 (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
kvmw authored Dec 21, 2023
1 parent 524a158 commit 3ee7935
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@

package io.pivotal.spring.cloud.config.client;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Daniel Lavoie
*/
@RunWith(SpringRunner.class)
@Import(ConfigResourceClientAutoConfiguration.class)
@SpringBootTest(classes = DisabledConfigResourceClientTests.class)
@ActiveProfiles("integration-test")
Expand All @@ -44,8 +41,8 @@ public class DisabledConfigResourceClientTests {

@Test
public void configClientBeanShouldNotBeInjected() {
Assert.assertNotNull("Spring container is not initialized.", environment);
Assert.assertNull(plainTextConfigClient);
assertThat(environment).isNotNull();
assertThat(plainTextConfigClient).isNull();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,23 @@
import java.io.InputStreamReader;
import java.util.stream.Collectors;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.cloud.config.client.ConfigClientProperties;
import org.springframework.core.io.Resource;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* @author Daniel Lavoie
* @author Dylan Roberts
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConfigServerTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = { "spring.profiles.active=plaintext,native", "spring.cloud.config.enabled=true",
"eureka.client.enabled=false", "spring.config.import=optional:configserver:" })
Expand Down Expand Up @@ -72,8 +70,8 @@ public class OAuth2ConfigResourceClientTest {

private ConfigResourceClient configClient;

@Before
public void setUp() {
@BeforeEach
public void setup() {
configClientProperties = new ConfigClientProperties(new MockEnvironment());
configClientProperties.setName("app");
configClientProperties.setProfile(null);
Expand All @@ -83,32 +81,35 @@ public void setUp() {

@Test
public void shouldFindSimplePlainFile() {
Assert.assertEquals(NGINX_CONFIG, read(configClient.getPlainTextResource(null, "master", "nginx.conf")));
assertThat(read(configClient.getPlainTextResource(null, "master", "nginx.conf"))).isEqualTo(NGINX_CONFIG);

Assert.assertEquals(DEV_NGINX_CONFIG, read(configClient.getPlainTextResource("dev", "master", "nginx.conf")));
assertThat(read(configClient.getPlainTextResource("dev", "master", "nginx.conf"))).isEqualTo(DEV_NGINX_CONFIG);

configClientProperties.setProfile("test");
Assert.assertEquals(TEST_NGINX_CONFIG, read(configClient.getPlainTextResource(null, "master", "nginx.conf")));
assertThat(read(configClient.getPlainTextResource(null, "master", "nginx.conf"))).isEqualTo(TEST_NGINX_CONFIG);
}

@Test(expected = HttpClientErrorException.class)
@Test
public void missingConfigFileShouldReturnHttpError() {
configClient.getPlainTextResource(null, "master", "missing-config.xml");
assertThatThrownBy(() -> configClient.getPlainTextResource(null, "master", "missing-config.xml"))
.isInstanceOf(HttpClientErrorException.class);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void missingApplicationNameShouldCrash() {
configClientProperties.setName("");
configClient.getPlainTextResource(null, "master", "nginx.conf");
assertThatThrownBy(() -> configClient.getPlainTextResource(null, "master", "nginx.conf"))
.isInstanceOf(IllegalArgumentException.class);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void missingConfigServerUrlShouldCrash() {
configClientProperties.setUri(new String[] { "" });
configClient.getPlainTextResource(null, "master", "nginx.conf");
assertThatThrownBy(() -> configClient.getPlainTextResource(null, "master", "nginx.conf"))
.isInstanceOf(IllegalArgumentException.class);
}

public String read(Resource resource) {
private String read(Resource resource) {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
return buffer.lines().collect(Collectors.joining("\n"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootContextLoader;
Expand All @@ -32,7 +30,6 @@
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -80,7 +77,6 @@ protected ConfigurableEnvironment getEnvironment() {

}

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles({ "integration-test", "native" })
@ContextConfiguration(classes = TestVaultApplication.class, loader = VaultPropertySourceContextLoader.class)
Expand Down

0 comments on commit 3ee7935

Please sign in to comment.