Skip to content

Commit b290a44

Browse files
Use AssertJ over JUnit5 assertions
1 parent be1e5eb commit b290a44

17 files changed

+114
-126
lines changed

src/test/java/jenkins/plugins/nodejs/JCasCTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import static org.hamcrest.Matchers.hasProperty;
3232
import static org.hamcrest.Matchers.hasSize;
3333
import static org.hamcrest.Matchers.instanceOf;
34-
import static org.junit.jupiter.api.Assertions.assertEquals;
35-
import static org.junit.jupiter.api.Assertions.assertTrue;
34+
import static org.hamcrest.Matchers.is;
3635

3736
import java.util.List;
3837

@@ -69,18 +68,18 @@ private void checkConfigFile(Jenkins j) {
6968
assertThat(config, instanceOf(NPMConfig.class));
7069

7170
NPMConfig npmConfig = (NPMConfig) config;
72-
assertEquals("myComment", npmConfig.comment);
73-
assertEquals("myContent", npmConfig.content);
74-
assertEquals("myConfig", npmConfig.name);
75-
assertTrue(npmConfig.isNpm9Format());
71+
assertThat(npmConfig.comment, equalTo("myComment"));
72+
assertThat( npmConfig.content, equalTo("myContent"));
73+
assertThat(npmConfig.name, equalTo("myConfig"));
74+
assertThat(npmConfig.isNpm9Format(), is(true));
7675

7776
List<NPMRegistry> registries = npmConfig.getRegistries();
78-
assertEquals(1, registries.size());
77+
assertThat(registries, hasSize(1));
7978

8079
NPMRegistry registry = registries.get(0);
81-
assertTrue(registry.isHasScopes());
82-
assertEquals("myScope", registry.getScopes());
83-
assertEquals("registryUrl", registry.getUrl());
80+
assertThat(registry.isHasScopes(), is(true));
81+
assertThat(registry.getScopes(), equalTo("myScope"));
82+
assertThat(registry.getUrl(), equalTo("registryUrl"));
8483
}
8584

8685
private void checkInstallations(Jenkins j) {
@@ -89,7 +88,7 @@ private void checkInstallations(Jenkins j) {
8988
assertThat(installations, arrayWithSize(2));
9089

9190
ToolInstallation withInstaller = installations[0];
92-
assertEquals("myNode", withInstaller.getName());
91+
assertThat( withInstaller.getName(), equalTo("myNode"));
9392

9493
final DescribableList<ToolProperty<?>, ToolPropertyDescriptor> properties = withInstaller.getProperties();
9594
assertThat(properties, hasSize(1));

src/test/java/jenkins/plugins/nodejs/NodeJSBuildWrapperTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package jenkins.plugins.nodejs;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
27-
import static org.junit.jupiter.api.Assertions.assertEquals;
2827
import static org.mockito.ArgumentMatchers.any;
2928
import static org.mockito.Mockito.RETURNS_SELF;
3029
import static org.mockito.Mockito.doReturn;
@@ -205,10 +204,12 @@ private PathVerifier(NodeJSInstallation installation) {
205204
@Override
206205
public void verify(EnvVars env) {
207206
String expectedValue = installation.getHome();
208-
assertEquals(expectedValue, env.get(NodeJSConstants.ENVVAR_NODEJS_HOME), "Unexpected value for " + NodeJSConstants.ENVVAR_NODEJS_HOME);
209-
assertThat(env.get("PATH")).contains(expectedValue);
210-
// check that PATH is not exact the NodeJS home otherwise means PATH was overridden
211-
assertThat(env.get("PATH")).isNotEqualTo(expectedValue); // JENKINS-41947
207+
assertThat(env)
208+
.as("Unexpected value for " + NodeJSConstants.ENVVAR_NODEJS_HOME).containsEntry(NodeJSConstants.ENVVAR_NODEJS_HOME, expectedValue);
209+
assertThat(env.get("PATH"))
210+
.contains(expectedValue)
211+
// JENKINS-41947 check that PATH is not exact the NodeJS home otherwise means PATH was overridden
212+
.isNotEqualTo(expectedValue);
212213
}
213214
}
214215

src/test/java/jenkins/plugins/nodejs/NodeJSCommandInterpreterTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
package jenkins.plugins.nodejs;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
27-
import static org.junit.jupiter.api.Assertions.*;
2827
import static org.mockito.ArgumentMatchers.any;
2928
import static org.mockito.Mockito.RETURNS_SELF;
3029
import static org.mockito.Mockito.doCallRealMethod;
@@ -76,13 +75,13 @@ static void setUp(JenkinsRule rule) {
7675
void test_inject_path_variable() throws Exception {
7776
NodeJSInstallation installation = mockInstaller();
7877
NodeJSCommandInterpreter builder = CIBuilderHelper.createMock("test_executable_value", installation, null, (build, launcher, listener) -> {
79-
assertFalse(build.getEnvironments().isEmpty(), "No Environments");
78+
assertThat(build.getEnvironments()).as("No Environments").isNotEmpty();
8079

8180
EnvVars env = build.getEnvironment(listener);
82-
assertThat(env).containsKeys(NodeJSConstants.ENVVAR_NODEJS_PATH, NodeJSConstants.ENVVAR_NODEJS_HOME);
83-
assertEquals(getTestHome(), env.get(NodeJSConstants.ENVVAR_NODEJS_HOME));
84-
assertEquals(getTestHome(), env.get(NodeJSConstants.ENVVAR_NODE_HOME));
85-
assertEquals(getTestBin(), env.get(NodeJSConstants.ENVVAR_NODEJS_PATH));
81+
assertThat(env)
82+
.containsEntry(NodeJSConstants.ENVVAR_NODEJS_HOME, getTestHome())
83+
.containsEntry(NodeJSConstants.ENVVAR_NODE_HOME, getTestHome())
84+
.containsEntry(NodeJSConstants.ENVVAR_NODEJS_PATH, getTestBin());
8685
});
8786

8887
FreeStyleProject job = j.createFreeStyleProject();
@@ -100,7 +99,7 @@ void test_executable_value() throws Exception {
10099
j.assertBuildStatusSuccess(job.scheduleBuild2(0));
101100

102101
String[] buildCommandLine = builder.buildCommandLine(new FilePath(File.createTempFile("junit", null, folder)));
103-
assertEquals(buildCommandLine[0], getTestExecutable());
102+
assertThat(buildCommandLine[0]).isEqualTo(getTestExecutable());
104103
}
105104

106105
@Test
@@ -114,9 +113,9 @@ void test_creation_of_config() throws Exception {
114113
String var = NodeJSConstants.NPM_USERCONFIG;
115114
String value = env.get(var);
116115

117-
assertTrue(env.containsKey(var), "variable " + var + " not set");
118-
assertNotNull(value, "empty value for environment variable " + var);
119-
assertTrue(new File(value).isFile(), "file of variable " + var + " does not exists or is not a file");
116+
assertThat(env.containsKey(var)).as("variable " + var + " not set").isTrue();
117+
assertThat(value).as("empty value for environment variable " + var).isNotNull();
118+
assertThat(new File(value).isFile()).as("file of variable " + var + " does not exists or is not a file").isTrue();
120119
});
121120

122121
FreeStyleProject job = j.createFreeStyleProject();

src/test/java/jenkins/plugins/nodejs/NpmrcFileSupplyTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
*/
2424
package jenkins.plugins.nodejs;
2525

26-
import static org.junit.jupiter.api.Assertions.assertEquals;
27-
import static org.junit.jupiter.api.Assertions.assertTrue;
28-
2926
import java.io.File;
3027
import java.util.ArrayList;
3128
import java.util.Arrays;
@@ -51,6 +48,8 @@
5148
import jenkins.plugins.nodejs.configfiles.Npmrc;
5249
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
5350

51+
import static org.assertj.core.api.Assertions.assertThat;
52+
5453
@WithJenkins
5554
class NpmrcFileSupplyTest {
5655

@@ -72,12 +71,12 @@ void test_supply_npmrc_with_registry() throws Exception {
7271
FreeStyleBuild build = j.buildAndAssertSuccess(j.createFreeStyleProject());
7372

7473
FilePath npmrcFile = ConfigFileManager.provisionConfigFile(new ConfigFile(config.id, null, true), null, build, build.getWorkspace(), j.createTaskListener(), new ArrayList<>(1));
75-
assertTrue(npmrcFile.exists());
76-
assertTrue(npmrcFile.length() > 0);
74+
assertThat(npmrcFile.exists()).isTrue();
75+
assertThat(npmrcFile.length()).isGreaterThan(0);
7776

7877
Npmrc npmrc = Npmrc.load(new File(npmrcFile.getRemote()));
79-
assertTrue(npmrc.contains("email"), "Missing setting email");
80-
assertEquals("[email protected]", npmrc.get("email"), "Unexpected value from settings email");
78+
assertThat(npmrc.contains("email")).as("Missing setting email").isTrue();
79+
assertThat(npmrc.get("email")).as("Unexpected value from settings email").isEqualTo("[email protected]");
8180
}
8281

8382
private StandardUsernameCredentials createUser(String id, String username, String password) throws FormException {

src/test/java/jenkins/plugins/nodejs/SimpleNodeJSCommandInterpreterTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@
3737
import java.io.IOException;
3838
import java.util.Collections;
3939

40-
import static org.junit.jupiter.api.Assertions.assertEquals;
41-
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
42-
import static org.junit.jupiter.api.Assertions.assertNotNull;
43-
import static org.junit.jupiter.api.Assertions.assertNull;
44-
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
import static org.assertj.core.api.Assertions.assertThat;
4541

4642
@WithJenkins
4743
class SimpleNodeJSCommandInterpreterTest {
@@ -64,38 +60,39 @@ void setUp() {
6460

6561
@Test
6662
void testGetContentsShouldGiveExpectedValue() {
67-
assertEquals(COMMAND, interpreter.getCommand());
63+
assertThat(interpreter.getCommand()).isEqualTo(COMMAND);
6864
}
6965

7066
@Test
7167
void testGetContentWithEmptyCommandShouldGiveExpectedValue() {
72-
assertEquals("", new NodeJSCommandInterpreter("", installation.getName(), null).getCommand());
68+
assertThat(new NodeJSCommandInterpreter("", installation.getName(), null).getCommand()).isEmpty();
7369
}
7470

7571
@Test
7672
void testGetContentWithNullCommandShouldGiveExpectedValue() {
77-
assertNull(new NodeJSCommandInterpreter(null, installation.getName(), null).getCommand());
73+
assertThat(new NodeJSCommandInterpreter(null, installation.getName(), null).getCommand()).isNull();
7874
}
7975

8076
@Test
8177
void testGetFileExtensionShouldGiveExpectedValue() throws IOException, InterruptedException {
82-
assertTrue(interpreter.createScriptFile(new FilePath(newFolder(tempFolder, "junit"))).getName().endsWith(".js"));
78+
assertThat(interpreter.createScriptFile(new FilePath(newFolder(tempFolder, "junit"))).getName()).endsWith(".js");
8379
}
8480

8581
@Test
8682
void testGetDescriptorShouldGiveExpectedValue() {
87-
assertNotNull(descriptor);
88-
assertInstanceOf(Descriptor.class, descriptor);
83+
assertThat(descriptor)
84+
.isNotNull()
85+
.isInstanceOf(Descriptor.class);
8986
}
9087

9188
@Test
9289
void testDescriptorGetDisplayNameShouldGiveExpectedValue() {
93-
assertEquals(Messages.NodeJSCommandInterpreter_displayName(), descriptor.getDisplayName());
90+
assertThat(descriptor.getDisplayName()).isEqualTo(Messages.NodeJSCommandInterpreter_displayName());
9491
}
9592

9693
@Test
9794
void testDescriptorGetHelpFileShouldGiveExpectedValue() {
98-
assertEquals("/plugin/nodejs/help.html", descriptor.getHelpFile());
95+
assertThat(descriptor.getHelpFile()).isEqualTo("/plugin/nodejs/help.html");
9996
}
10097

10198
private static File newFolder(File root, String... subDirs) throws IOException {

src/test/java/jenkins/plugins/nodejs/VerifyEnvVariableBuilder.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package jenkins.plugins.nodejs;
2525

26-
import static org.junit.jupiter.api.Assertions.*;
2726

2827
import java.io.File;
2928
import java.io.IOException;
@@ -34,6 +33,8 @@
3433
import hudson.model.BuildListener;
3534
import hudson.tasks.Builder;
3635

36+
import static org.assertj.core.api.Assertions.assertThat;
37+
3738
abstract class VerifyEnvVariableBuilder extends Builder {
3839
@Override
3940
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
@@ -50,9 +51,9 @@ public void verify(EnvVars env) {
5051
String var = NodeJSConstants.NPM_USERCONFIG;
5152
String value = env.get(var);
5253

53-
assertTrue(env.containsKey(var), "variable " + var + " not set");
54-
assertNotNull(value, "empty value for environment variable " + var);
55-
assertTrue(new File(value).isFile(), "file of variable " + var + " does not exists or is not a file");
54+
assertThat(env).as("variable " + var + " not set").containsKey(var);
55+
assertThat(value).as("empty value for environment variable " + var).isNotNull();
56+
assertThat(new File(value)).as("file of variable " + var + " does not exists or is not a file").isFile();
5657
}
5758
}
5859

@@ -68,10 +69,7 @@ public EnvVarVerifier(String name, String value) {
6869

6970
@Override
7071
public void verify(EnvVars env) {
71-
String envValue = env.get(name);
72-
73-
assertTrue(env.containsKey(name), "variable " + name + " not set");
74-
assertEquals(value, envValue);
72+
assertThat(env).as("variable " + name + " not set").containsEntry(name, value);
7573
}
7674
}
7775
}

src/test/java/jenkins/plugins/nodejs/cache/CacheLocationLocatorTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
import org.junit.jupiter.api.io.TempDir;
3333
import org.mockito.MockedStatic;
3434

35-
import static org.junit.jupiter.api.Assertions.assertEquals;
36-
import static org.junit.jupiter.api.Assertions.assertNull;
35+
import static org.assertj.core.api.Assertions.assertThat;
3736
import static org.mockito.Mockito.mock;
3837
import static org.mockito.Mockito.mockStatic;
3938
import static org.mockito.Mockito.when;
@@ -54,12 +53,12 @@ void setup() throws Exception {
5453

5554
@Test
5655
void test_default() {
57-
assertNull(new DefaultCacheLocationLocator().locate(workspace), "expect null location path");
56+
assertThat(new DefaultCacheLocationLocator().locate(workspace)).as("expect null location path").isNull();
5857
}
5958

6059
@Test
6160
void test_per_job() {
62-
assertEquals(workspace.child(".npm"), new PerJobCacheLocationLocator().locate(workspace), "expect the same location path passes as input");
61+
assertThat(new PerJobCacheLocationLocator().locate(workspace)).as("expect the same location path passes as input").isEqualTo(workspace.child(".npm"));
6362
}
6463

6564
@Test
@@ -80,7 +79,7 @@ void test_per_executor() throws Exception {
8079
when(wc.toComputer()).thenReturn(computer);
8180

8281
FilePath expectedLocation = rootPath.child("npm-cache").child(String.valueOf(executorNumber));
83-
assertEquals(expectedLocation, new PerExecutorCacheLocationLocator().locate(wc), "expect null location path");
82+
assertThat(new PerExecutorCacheLocationLocator().locate(wc)).as("expect null location path").isEqualTo(expectedLocation);
8483
}
8584
}
8685

src/test/java/jenkins/plugins/nodejs/configfiles/NPMConfigTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.jenkinsci.lib.configprovider.model.Config;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29-
import static org.junit.jupiter.api.Assertions.assertNotNull;
3029

3130
import org.junit.jupiter.api.Test;
3231
import org.jvnet.hudson.test.JenkinsRule;
@@ -41,13 +40,14 @@ class NPMConfigTest {
4140
@Test
4241
void test_load_template(JenkinsRule j) {
4342
Descriptor<?> descriptor = j.jenkins.getDescriptor(NPMConfig.class);
44-
assertNotNull(descriptor, "NPMConfig descriptor not registered");
45-
assertThat(descriptor).isInstanceOf(NPMConfigProvider.class).describedAs("Unexpected descriptor class");
43+
assertThat(descriptor)
44+
.as("NPMConfig descriptor not registered").isNotNull()
45+
.as("Unexpected descriptor class").isInstanceOf(NPMConfigProvider.class);
4646

4747
NPMConfigProvider provider = (NPMConfigProvider) descriptor;
4848
Config config = provider.newConfig("testId");
49-
assertThat(config).isInstanceOf(NPMConfig.class).describedAs("Unexpected config class");
50-
assertThat(config.content).isNotBlank().describedAs("Expected the default template, instead got empty");
49+
assertThat(config).as("Unexpected config class").isInstanceOf(NPMConfig.class);
50+
assertThat(config.content).as("Expected the default template, instead got empty").isNotBlank();
5151
}
5252

5353
}

src/test/java/jenkins/plugins/nodejs/configfiles/NPMConfigValidationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
package jenkins.plugins.nodejs.configfiles;
2525

26+
import static org.assertj.core.api.Assertions.assertThat;
2627
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
27-
import static org.junit.jupiter.api.Assertions.*;
2828

2929
import java.util.Arrays;
3030
import java.util.List;
@@ -37,11 +37,11 @@ class NPMConfigValidationTest {
3737
void test_new_config() {
3838
String id = "test_id";
3939
NPMConfig config = new NPMConfig(id, "", "", "", null);
40-
assertEquals(id, config.id);
41-
assertNull(config.name);
42-
assertNull(config.comment);
43-
assertNull(config.content);
44-
assertNotNull(config.getRegistries());
40+
assertThat(config.id).isEqualTo(id);
41+
assertThat(config.name).isNull();
42+
assertThat(config.comment).isNull();
43+
assertThat(config.content).isNull();
44+
assertThat(config.getRegistries()).isNotNull();
4545
}
4646

4747
@Test

0 commit comments

Comments
 (0)