Results 1 to 10 of 10
  1. #1
    TaniaG. is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Questions about my code and JUnit Test

    Hi..!
    Sorry for my English first..!
    I have wrote this class who read from text line by line and save the words in fileOnTable.. Now i don't know what to read in ReadOffer to save the words in object offers and return this.. One more question.. What JUnit test can write for this code..?


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    import com.example.crazysellout.Offer;

    //Class that, reads the file of the data.
    public class OfferDataReader {
    String[] fileOnTable;

    Offer ReadOffer(int indexLine){
    Offer offers = new Offer();

    offers.StoreName = fileOnTable[0];
    offers.ProductCategory = fileOnTable[1];
    offers.ProductName = fileOnTable[2];
    offers.ProductPrice = fileOnTable[3];
    offers.ProductDescription = fileOnTable[4];

    return offers;
    }

    //initialize txt reader with the input stream to set fileOnTable
    public OfferDataReader(InputStream iStream) throws IOException {
    super();
    this.readDataFile(iStream);
    }

    //Method that reads the text file from resources and
    public void readDataFile(InputStream iStream) throws IOException {
    //String that gets each line of the text file in a while loop
    String stringContainer = null;

    //Variable in which the contex of the file will be stored
    StringBuffer strBuffer = new StringBuffer();

    //Reads each line from the file.
    BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));

    while ((stringContainer = reader.readLine()) != null) {
    strBuffer.append(stringContainer + "\n" );
    }
    iStream.close();
    textIndexOnTable(strBuffer.toString());
    }

    //Method that separates the string buffer to lines, and saves
    //on a table so that they can be identified as an account.
    public void textIndexOnTable(String indexOnString){
    fileOnTable = indexOnString.split("\r\n|\r|\n");
    }

    //Method that breaks each line of the table in offers
    public void SplitLines (String splitLinesOnTable){
    fileOnTable = splitLinesOnTable.split(" ");
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Questions about my code and JUnit Test

    When posting code please wrap it in [code] tags [/code] so it retains its formatting and is easier to read.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    TaniaG. is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Questions about my code and JUnit Test

    Questions about my code and JUnit Test-fotoflexer_photo.jpg

    Is it better?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Questions about my code and JUnit Test

    code tags are preferable to an image as it's not possible to copy/paste from an image...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    TaniaG. is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Questions about my code and JUnit Test

    Java Code:
    package com.example.crazysellout.UserSide;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import com.example.crazysellout.Offer;
    
    //Class that, reads the file of the data.
    public class OfferDataReader {
    	String[] fileOnTable;
    	
    	Offer ReadOffer(int indexLine){
    		Offer offers = new Offer();
    		
    		offers.StoreName = fileOnTable[0];		
    		offers.ProductCategory = fileOnTable[1];
    		offers.ProductName = fileOnTable[2];
    		offers.ProductPrice = fileOnTable[3];
    		offers.ProductDescription = fileOnTable[4];
    		
    		return offers;
    	}
    	
    	//initialize txt reader with the input stream to set fileOnTable
    	public OfferDataReader(InputStream iStream) throws IOException {
    		super();
    		this.readDataFile(iStream);
    	}
    	
    	//Method that reads the text file from resources and 
    	public void readDataFile(InputStream iStream) throws IOException {
    		//String that gets each line of the text file in a while loop
    		String stringContainer = null;
    			
    		//Variable in which the contex of the file will be stored
    		StringBuffer strBuffer = new StringBuffer();
    		
    		//Reads each line from the file.
    		BufferedReader reader = new BufferedReader(new InputStreamReader(iStream));
    		
    		while ((stringContainer = reader.readLine()) != null) {
    			strBuffer.append(stringContainer + "\n" );
    		}
    		iStream.close();
    		textIndexOnTable(strBuffer.toString());
    	}
    	
    	//Method that separates the string buffer to lines, and saves
    	//on a table so that they can be identified as an account.
    	public void textIndexOnTable(String indexOnString){
    		fileOnTable = indexOnString.split("\r\n|\r|\n");
    	}
    	
    	//Method that breaks each line of the table in offers
    	public void SplitLines (String splitLinesOnTable){
    		fileOnTable = splitLinesOnTable.split(" ");
    	}
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Questions about my code and JUnit Test

    OK.
    So what is the problem you have with ReadOffer (by the Java code conventions that should be readOffer, but that's a minor point)?
    You seem to be populating the 'offers' object and returning it, which is all you seem to want to do.

    As for unit testing, you'd write a set of tests for each public method to check it is doing what the method is defined as doing.
    That is, it either returns what is expected, or changes some value as expected.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    TaniaG. is offline Member
    Join Date
    Nov 2014
    Posts
    4
    Rep Power
    0

    Default Re: Questions about my code and JUnit Test

    My question about ReadOffer is if this who i wrote return right the offers..! Because i'm not sure about that..!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Questions about my code and JUnit Test

    return right the offers..! Because i'm not sure about that
    How can the values in the Offer object that is returned be tested to see if it is correct?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Questions about my code and JUnit Test

    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Questions about my code and JUnit Test

    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 02-11-2013, 04:55 PM
  2. problem with JUnit test
    By exltus in forum Advanced Java
    Replies: 12
    Last Post: 12-19-2011, 07:49 PM
  3. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  4. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 05:15 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •