Loading ...
Sorry, an error occurred while loading the content.

Re: [junit] Junit 4 help

Expand Messages
  • David Saff
    Madhavi, Have you looked at the Parameters runner in JUnit 4? There s an example here:
    Message 1 of 2 , Nov 2, 2009
    • 0 Attachment
      Madhavi,

      Have you looked at the Parameters runner in JUnit 4? There's an example here:

      http://weblogs.java.net/blog/fabianocruz/archive/2006/06/junit_4_you_1.html

      Good luck,

      David Saff

      On Fri, Oct 30, 2009 at 11:17 PM, madhavi fuge <madhufuge@...> wrote:
      > Hi All,
      > I am a beginner in Junit4. I need your help to write a parameterised
      > Junit test case. This will help me in learning Junit in a better way. I
      > am trying to do data-driven testing in Junit4. I am reading data from
      > CSV file and using it to run the test case. In the attached files I have a read CSV file that reads data from CSV file and VO file creates a array of the CSV file data. The    public static Collection data() method of example1Test with the help of getter, gets the datafor the test.   But When I run the testcase,  I get a exception, the trace is:
      >
      >
      >
      > java.lang.Exception: Method data()
      > should be void
      >             at
      > org.junit.runners.model.FrameworkMethod.validatePublicVoid(FrameworkMethod.java:90)
      >             at
      > org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(FrameworkMethod.java:66)
      >             at
      > org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(ParentRunner.java:111)
      >             at
      > org.junit.runners.ParentRunner.collectInitializationErrors(ParentRunner.java:92)
      >             at
      > org.junit.runners.ParentRunner.validate(ParentRunner.java:253)
      >             at
      > org.junit.runners.ParentRunner.<init>(ParentRunner.java:55)
      >             at
      > org.junit.runners.Suite.<init>(Suite.java:99)
      >             at
      > org.junit.runners.Parameterized.<init>(Parameterized.java:133)
      >             at
      > sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
      > Method)
      >             at
      > sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
      > Source)
      >             at
      > sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
      > Source)
      >             at
      > java.lang.reflect.Constructor.newInstance(Unknown Source)
      >             at
      > org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
      >             at
      > org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
      >             at
      > org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      >             at
      > org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
      >             at
      > org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      >             at
      > org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
      >             at
      > org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:29)
      >             at
      > org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
      >             at
      > org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
      >             at
      > org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
      >             at
      > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
      >             at
      > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      >             at
      > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      >             at
      > org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      >
      >
      > As per my understanding the Read csv
      > class shud be executed before the TESTCASE class only then the test data will be
      > available.
      > I understand that I need to use
      > @Before annotation but I do not know how to use it.  I have attached the files
      > for your reference. The VO file has the logic to pass the csv data as array.  I
      > have also attached the CSV jar for reading the CSV file.
      >
      > Please can you help me further? What
      > should I write in @Before for my case to work?
      >
      > Thanks,Madhavi
      >
      >
      >
      >
      >  ----------
      >
      > package com;
      >
      > import java.util.Iterator;
      > import java.util.List;
      >
      > public class VO {
      >        private String[][] dataObject;
      >
      >        public String[][] getDataObject() {
      >                return this.dataObject;
      >        }
      >
      >        public void setDataObject(List valueList) {
      >                Iterator it = valueList.iterator();
      >                String[][] dataObjectArray = null;
      >                int i=0;
      >                while(it.hasNext()){
      >                        String row = (String)it.next();
      >                        System.out.println("Row: "+row);
      >                        String methodName=null,remainingString=null,val1=null,remainingString1=null,val2=null,expVal=null,remainingString2=null;
      >                        String[] sTempArr=null;
      >
      >                //Validate Date String
      >                sTempArr = row.split(",");
      >                if (sTempArr.length!=2)
      >                {
      >
      >                }
      >
      >                methodName =sTempArr[0];
      >                val1 =sTempArr[1];
      >                val2 = sTempArr[2];
      >                expVal = sTempArr[3];;
      >                        dataObjectArray = new String[20][20];
      >                        if("intAdd".equalsIgnoreCase(methodName)){
      >
      >                                                dataObjectArray[i][0] = expVal;
      >                                                dataObjectArray[i][1] = val1;
      >                                                dataObjectArray[i][2] = val2;
      >                        }
      >                 i++;
      >                }
      >                this.dataObject = dataObjectArray;
      >        }
      > }
      >
      >
      >
      > [Non-text portions of this message have been removed]
      >
      >
      >
      > ------------------------------------
      >
      > Yahoo! Groups Links
      >
      >
      >
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.