From 342cd217eb913153249dba289669c98bef4b9d42 Mon Sep 17 00:00:00 2001 From: giriharan13 Date: Sun, 24 Sep 2023 14:55:43 +0530 Subject: [PATCH] testing Parameterized tests in JUnit4 --- junit-4-basics/.classpath | 2 +- .../.settings/org.eclipse.jdt.core.prefs | 13 ++++-- .../main/java/com/giriharan/StringUtils.java | 17 +++++++ .../com/giriharan/StringUtilsParamTest.java | 45 +++++++++++++++++++ 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 junit-4-basics/src/main/java/com/giriharan/StringUtils.java create mode 100644 junit-4-basics/src/test/java/com/giriharan/StringUtilsParamTest.java diff --git a/junit-4-basics/.classpath b/junit-4-basics/.classpath index 51a6fca..d6383c6 100644 --- a/junit-4-basics/.classpath +++ b/junit-4-basics/.classpath @@ -26,7 +26,7 @@ - + diff --git a/junit-4-basics/.settings/org.eclipse.jdt.core.prefs b/junit-4-basics/.settings/org.eclipse.jdt.core.prefs index a3b98fd..db24ee7 100644 --- a/junit-4-basics/.settings/org.eclipse.jdt.core.prefs +++ b/junit-4-basics/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,15 @@ eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/junit-4-basics/src/main/java/com/giriharan/StringUtils.java b/junit-4-basics/src/main/java/com/giriharan/StringUtils.java new file mode 100644 index 0000000..e3a07e3 --- /dev/null +++ b/junit-4-basics/src/main/java/com/giriharan/StringUtils.java @@ -0,0 +1,17 @@ +package com.giriharan; + +import java.util.stream.IntStream; + +public class StringUtils { + + String addString(String s1,String s2) + { + return s1+s2; + } + + int countVowels(String s) + { + return (int) s.chars().filter(arg -> arg=='a' || arg=='e' || arg=='i' || arg=='o' || arg=='u').count(); + } + +} diff --git a/junit-4-basics/src/test/java/com/giriharan/StringUtilsParamTest.java b/junit-4-basics/src/test/java/com/giriharan/StringUtilsParamTest.java new file mode 100644 index 0000000..5ed30d7 --- /dev/null +++ b/junit-4-basics/src/test/java/com/giriharan/StringUtilsParamTest.java @@ -0,0 +1,45 @@ +package com.giriharan; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class StringUtilsParamTest { + + StringUtils su; + String s1,s2,expected; + + @Before + public void init() + { + su = new StringUtils(); + } + + public StringUtilsParamTest(String s1,String s2,String res) + { + this.s1 = s1; + this.s2 = s2; + this.expected = res; + } + + + @Parameters + public static Object[] testAddValues() { + return new Object[] { + new Object[] {"Hello","World","HelloWorld"}, + new Object[] {"","world" ,"world"}, + new Object[] {"hello","","hello"} + }; + } + + @Test + public void testAdd() { + assertEquals(expected,su.addString(s1, s2)); + } + +}