Skip to content

Commit

Permalink
refactor: JUnit Jupiter migration from JUnit 4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
aamotharald and TeamModerne committed Nov 25, 2024
1 parent 64dac8f commit e1ca755
Show file tree
Hide file tree
Showing 84 changed files with 601 additions and 518 deletions.
14 changes: 14 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<profiles>
<profile>
<id>release</id>
Expand Down
18 changes: 6 additions & 12 deletions env/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@
<artifactId>spotbugs-annotations</artifactId>
<optional>true</optional>
</dependency>

<!-- test utilities -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -80,6 +68,12 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
package com.sap.cloud.security.config;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.Collections;
Expand All @@ -19,7 +19,7 @@ public class OAuth2ServiceConfigurationBuilderTest {

private OAuth2ServiceConfigurationBuilder cut;

@Before
@BeforeEach
public void setUp() {
cut = OAuth2ServiceConfigurationBuilder.forService(Service.XSUAA);
}
Expand Down Expand Up @@ -221,4 +221,4 @@ public void x509IntegrationTest() {
assertThat(configuration.getCertUrl()).isEqualTo(URI.create(url));
assertThat(configuration.getCredentialType()).isEqualTo(CredentialType.X509);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/
package com.sap.cloud.security.json;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.time.LocalDate;
Expand All @@ -31,7 +31,7 @@ public class DefaultJsonObjectTest {

private DefaultJsonObject cut;

@Before
@BeforeEach
public void setUp() {
cut = createJsonParser(KEY_1, STRING_VALUE);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ public void notVulnerableToStackOverflowException() {
try {
new DefaultJsonObject(deeplyNestedJsonObject);
} catch (StackOverflowError e) {
Assert.fail("Encountered StackoverflowError.");
Assertions.fail("Encountered StackoverflowError.");
} catch (Exception ignored) {
}
}
Expand All @@ -251,4 +251,4 @@ private DefaultJsonObject createJsonParser(String key, Object value) {
private String createJsonObjectString(String key, Object value) {
return String.format("{%s : %s}", key, value);
}
}
}
18 changes: 6 additions & 12 deletions java-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- test utilities -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand All @@ -65,6 +53,12 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
package com.sap.cloud.security.config;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public class ClientCredentialsTest {
ClientIdentity cut = new ClientCredentials("clientId", "clientSecret");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
*/
package com.sap.cloud.security.config;

import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class CredentialTypeTest {

@Test
public void from() {
Assert.assertEquals(CredentialType.X509, CredentialType.from("x509"));
Assert.assertEquals(CredentialType.X509_GENERATED, CredentialType.from("X509_GENERATED"));
Assert.assertEquals(CredentialType.X509_PROVIDED, CredentialType.from("X509_PROVIDED"));
Assert.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("X509_ATTESTED"));
Assert.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("x509_attested"));
Assert.assertEquals(CredentialType.INSTANCE_SECRET, CredentialType.from("instance-secret"));
Assert.assertEquals(CredentialType.BINDING_SECRET, CredentialType.from("binding-secret"));
Assertions.assertEquals(CredentialType.X509, CredentialType.from("x509"));
Assertions.assertEquals(CredentialType.X509_GENERATED, CredentialType.from("X509_GENERATED"));
Assertions.assertEquals(CredentialType.X509_PROVIDED, CredentialType.from("X509_PROVIDED"));
Assertions.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("X509_ATTESTED"));
Assertions.assertEquals(CredentialType.X509_ATTESTED, CredentialType.from("x509_attested"));
Assertions.assertEquals(CredentialType.INSTANCE_SECRET, CredentialType.from("instance-secret"));
Assertions.assertEquals(CredentialType.BINDING_SECRET, CredentialType.from("binding-secret"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package com.sap.cloud.security.config;

import org.junit.Test;
import org.junit.jupiter.api.Test;

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

Expand All @@ -21,4 +21,4 @@ public void getCFNameOfXsuaa_shouldReturnCorrectName() {
assertThat(Service.XSUAA.getCFName()).isEqualTo("xsuaa");
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
package com.sap.cloud.security.token;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class TokenTest {

Expand Down
16 changes: 12 additions & 4 deletions java-security-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
<groupId>com.sap.cloud.security</groupId>
<artifactId>java-security</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down Expand Up @@ -76,6 +72,18 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import com.sap.cloud.security.token.Token;
import com.sap.cloud.security.token.TokenClaims;
import com.sap.cloud.security.token.TokenHeader;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

import java.io.File;
Expand Down Expand Up @@ -52,15 +51,15 @@ public class JwtGeneratorTest {

private static final Path RESOURCES_PATH = Paths.get(JwtGeneratorTest.class.getResource("/").getPath());

@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder(RESOURCES_PATH.toFile());
@TempDir
public static File temporaryFolder;

@BeforeClass
@BeforeAll
public static void setUpClass() throws Exception {
keys = RSAKeys.fromKeyFiles("/publicKey.txt", "/privateKey.txt");
}

@Before
@BeforeEach
public void setUp() {
cut = JwtGenerator.getInstance(XSUAA, DEFAULT_CLIENT_ID)
.withPrivateKey(keys.getPrivate());
Expand Down Expand Up @@ -293,7 +292,7 @@ public void withClaimValue_asJsonObjectContainingList() {

@Test
public void loadClaimsFromFile_doesNotContainValidJson_throwsException() throws IOException {
File emptyFile = temporaryFolder.newFile("empty");
File emptyFile = File.createTempFile("empty", null, temporaryFolder);
String temporaryFolderName = emptyFile.getParentFile().getName();
String resourcePath = "/" + temporaryFolderName + "/empty";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
package com.sap.cloud.security.test;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -31,4 +31,4 @@ public void fromKeyFiles() throws IOException, InvalidKeySpecException,
assertThat(keys.getPrivate()).isNotNull();
assertThat(keys.getPublic()).isNotNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import org.eclipse.jetty.ee10.servlet.ServletHolder;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.IOException;
Expand Down Expand Up @@ -152,7 +153,8 @@ public void getContext() {
assertThat(cut.getContext()).isNotNull();
}

public static class SecurityTestRuleWithMockServlet {
@Nested
public class SecurityTestRuleWithMockServlet {

private HttpServlet mockServlet = Mockito.mock(HttpServlet.class);

Expand All @@ -172,7 +174,8 @@ public void testThatServletMethodIsNotCalled() throws ServletException, IOExcept

}

public static class SecurityTestRuleWithoutApplicationServer {
@Nested
public class SecurityTestRuleWithoutApplicationServer {

@Rule
public SecurityTestRule rule = SecurityTestRule.getInstance(XSUAA);
Expand All @@ -184,7 +187,8 @@ public void testRuleIsInitializedCorrectly() {
}
}

public static class SecurityTestRuleApplicationServer_IAS {
@Nested
public class SecurityTestRuleApplicationServer_IAS {

@Rule
public SecurityTestRule rule = SecurityTestRule.getInstance(IAS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package com.sap.cloud.security.test;

import com.sap.cloud.security.config.Service;
import org.junit.Test;
import org.junit.jupiter.api.Test;

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

Expand All @@ -21,4 +21,4 @@ public void wireMockServerIsNotRunningAfterTearDown() throws Exception {
assertThat(cut.wireMockServer.isRunning()).isFalse();
}

}
}
Loading

0 comments on commit e1ca755

Please sign in to comment.