Reputation: 1253
I was preparing a test case scenario where I put the values to be tested against their keys in properties
file. I wanted if all key values satisfied with their corresponding values, the method should return true, else false.
Below is my code:
package mypackTest;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertThat;
@RunWith(Parameterized.class)
public class Test123 {
static Properties p = null;
private boolean expected;
@Parameters
public static List<String[]> data()
{
String[][] data = new String[][] { { "D:\\personal\\config.properties" }};
return Arrays.asList(data);
}
@BeforeClass
public static void setup()
{
p = new Properties();
try
{
//load a properties file
p.load(new FileReader(new File("D:\\personal\\config.properties")));
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
@Test
public void do_test() {
assertThat(TestThis.letstest(p.getProperty("abc1"), p.getProperty("abc2")), is(expected));
}
}
the class that I wanted to test upon:
package mypackTest;
public class TestThis {
public static boolean letstest(String abc1,String abc2){
if(abc1.equals("xyz1")&& abc2.equals("xyz2")){
return true;
}
return false;
}
}
properties file
abc1=xyz1
abc2=xyz2
TestRunner class
package mypackTest;
import java.util.Enumeration;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import junit.framework.TestFailure;
import junit.framework.TestResult;
import junit.framework.TestSuite;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(CalculatorTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
I am getting below output
OUTPUT
do_test0: wrong number of arguments
false
Upvotes: 0
Views: 1337
Reputation: 140417
There are various problems with this code. First of all:
private boolean expected;
You are declaring that field - but you never assign a value to it. Thus it is initialized to false
and stays false
. So don't expect that it comparing it to something that is true
would work out.
But beyond that: the way you are using Parametrized
doesn't make any sense. The idea of this special runner is: the @Parameters "thing" contains multiple values; and your test is invoked in a loop for each entry. Having a single file name there - that is like "buying for a truck to then transport a single suitcase". Meaning: you would rather specify a list of key/value entries there; and then (for example) make sure that a property file contains all these entries. Or something like that.
So the real answer is: step back and learn about the basics. For example your usage of static is also implying that you have no understanding of what static actually is, and when to use it. And then follow a tutorial for JUnit (normal JUnit). And when you figured how to use that properly, then read a tutorial about @Parametrized.
In other words: don't apply "trial and error" when you are lacking basic understanding. This strategy only works when you have enough experience to not always error immediately for anything you try.
Upvotes: 1