Results 1 to 5 of 5
  1. #1
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Unhappy Junit 4. failure(s)

    following Mark Dexter's persistence tutorial, im at the end of number 5.

    I have followed along and sort of get the gist of it.
    But, even though I have typed in the code as per the tutorial the test fails:confused:

    I should mention that I did have some problems initially getting Junit 4 to operate. and i also deleted Junit 3 from build path
    see here Mark Dexter persistence tutorial Junit problems explains perhaps this is a problem?

    there are a couple of things that I am unsure about within the code

    1. in the MyUtilitiestest class, a new file "testsavetostring.txt" is created, then immediately deleted. i think that this is ok, because the Junit test gets passed here.

    2, the problem (where the test fails) appears to be with the first assertTrue
    which passes the saveString to the MyUtilities.saveStringToFile method which should save the two lines of text (saveString) in the file "testsavestring.txt"
    this is a different filename,,, but i guess thats cos we deleted the first file already, but i kinda think the names should be the same because the first bit of code deletes the file if its already there, (according to what i understood from Mr Dexters explanation). but if it were to be there already why would you create it?? ive tried renaming the file names so they are all the same but to no avail. so i put them back the way they are in the tutorial. maybe and probably this is a side issue and nothing to do with why the test fails.
    I don't know?

    any help sugestions, gratefully recieved,
    thanks in advance

    here is the test class code

    Java Code:
    public class MyUtilitiesTest {
    	
    	@Test
    	public void saveStringToFile(){
    		String saveString = "this is test line one \n" + 
    		"this is test line two\n";
    		
    		File testFile = new File("testsavetostring.txt");
    		testFile.delete();
    		
    		assertFalse("File should not exist", 
    				testFile.exists());
    		
    		assertTrue("File should have been saved",
    				MyUtilities.saveStringToFile("testsavestring.txt",
    						saveString));
    		
    		String newString = MyUtilities.getStringFromFile(
    				"testsavestring.txt");
    		
    		assertTrue("Save and get strings should be equal ", 
    				saveString.equals(newString));
    		
    		assertFalse("File should not be saved", 
    				MyUtilities.saveStringToFile(
    						"non-existent directory/thishouldfail.txt",
    						saveString));
    		
    		String emptyString = MyUtilities.getStringFromFile(
    				"badfilename.txt");
    		
    		
    		assertTrue("String should be empty",
    				emptyString.length() == 0);
    
    	}
    
    }
    and here is the class under test

    Java Code:
    public class MyUtilities {
    
    	public static boolean saveStringToFile(String fileName, String saveString) {
    		boolean saved = false;
    		BufferedWriter bw = null;
    
    		try {
    			bw = new BufferedWriter(new FileWriter(fileName));
    
    			try {
    				bw.write(saveString);
    			} 
    			finally {
    				bw.close();
    			}
    		}
    
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    
    		return saved;
    	}
    
    	public static String getStringFromFile(String fileName) {
    		BufferedReader br = null;
    		StringBuilder sb = new StringBuilder();
    		
    		try{
    			br = new BufferedReader (
    					new FileReader(fileName));
    			try{
    				String s;
    				while(( s = br.readLine())!= null){
    					// add linefeed back since stripped by readline
    					sb.append(s);
    					sb.append("\n");
    				}
    				
    			}
    			finally{
    				br.close();
    			}
    		}
    		catch(IOException ex){
    			ex.printStackTrace();
    		}
    		return sb.toString();
    		
    	}
    
    }
    and here is the Junit failure trace.

    Java Code:
    java.lang.AssertionError: File should have been saved
    	at org.junit.Assert.fail(Assert.java:71)
    	at org.junit.Assert.assertTrue(Assert.java:34)
    	at org.persistence.tutorial.MyUtilitiesTest.saveStringToFile(MyUtilitiesTest.java:21)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99)
    	at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81)
    	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    	at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
    	at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
    	at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
    	at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
    	at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
    	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
    	at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Your saveStringToFile() method returns the boolean variable saved. The method sets it to false at the beginning, but shouldn't it be set to true after writing the file?

    -Gary-

  3. #3
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Red face whoops!!

    okay,, i see,, i missed that :o


    Java Code:
    try {
    				bw.write(saveString);
    				saved = true;
    			}
    thanks Gary,,

    if i hadnt had this niggle in my mind about the stanford eclipse, perhaps id have paid more attention to that, think am gonna put eclipse propper on at some point soon

    Kind regards
    sonny
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  4. #4
    tianyan is offline Member
    Join Date
    Aug 2010
    Posts
    1
    Rep Power
    0

    Default Follow up question (Failure on saveStringToFile)

    I'm having a similar problem following Mark Dexter's persistence tutorial. When I ran the JUnit4 test, saveStringToFile appeared in the failure trace. The code all still looked a little arcane to me so I don't know how to solve the problem.

    The failure trace gave me:
    Java Code:
    java.lang.AssertionError: Save and get strings should be equal
    	at org.junit.Assert.fail(Assert.java:91)
    	at org.junit.Assert.assertTrue(Assert.java:43)
    	at org.persistence.tutorial.MyUtilitiesTest.saveStringToFile(MyUtilitiesTest.java:25)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	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)

    The code for MyUtilitiesTest is
    Java Code:
    package org.persistence.tutorial;
    
    import java.io.File;
    
    import org.junit.*;
    import static org.junit.Assert.*;
    
    public class MyUtilitiesTest {
    	
    	@Test
    	public void saveStringToFile() {
    		String saveString = "this is test line one\n" +
    			"this is test line two\n;";
    		File testFile = new File("testsavetostring.txt");
    		testFile.delete();
    		assertFalse("File should not exist",
    				testFile.exists());
    		
    		assertTrue("File should have been saved",
    				MyUtilities.saveStringToFile("testsavestring.txt",
    						saveString));
    	
    		String newString = MyUtilities.getStringFromFile(
    				"testsavestring.txt");
    		assertTrue("Save and get strings should be equal",
    				saveString.equals(newString));
    		assertFalse("File should not be saved",
    				MyUtilities.saveStringToFile(
    						"non-existent directory/thisshouldfail.txt",
    						saveString));
    		
    		String emptyString = MyUtilities.getStringFromFile(
    				"badfilename.txt");
    		assertTrue("String should be empty",
    				emptyString.length() == 0);
    		
    	}
    	
    	
    	public MyLibrary createMyLibrary() {
    		Book b1;
    		Book b2;
    		Person p1;
    		Person p2;
    		MyLibrary ml;
    		
    		b1 = new Book("Book1");
    		b2 = new Book("Book2");
    		p1 = new Person();
    		p1.setName("Fred");
    		p2 = new Person();
    		p2.setName("Sue");
    		ml = new MyLibrary("Test");
    		
    		ml.addBook(b1);
    		ml.addBook(b2);
    		ml.addPerson(p1);
    		ml.addPerson(p2);
    		ml.checkOut(b1, p1);
    		
    		
    		return ml;
    
    	}
    	
    	@Test public void convertToXml() {
    		MyLibrary startMyLibrary = createMyLibrary();
    		String testXMLOut = MyUtilities.convertToXML(startMyLibrary);
    		MyLibrary endMyLibrary = 
    			MyUtilities.convertFromXML(testXMLOut);
    		assertEquals("Test", endMyLibrary.getName());
    		assertEquals(2, endMyLibrary.getBooks().size());
    		assertEquals(2, endMyLibrary.getPeople().size());
    		assertEquals("Fred", endMyLibrary.getBooks().get(0).getPerson().getName());
    	}
    	
    	@Test public void saveToXMLFile() {
    		MyLibrary startMyLibrary = createMyLibrary();
    		String fileName = "testmylibrary.xml";
    		File testFile = new File(fileName);
    		testFile.delete();
    		assertFalse("File should not exist", testFile.exists());
    		assertTrue("File should have been saved", MyUtilities.saveMyLibraryToXMLFile(fileName, startMyLibrary));
    		MyLibrary endMyLibrary = 
    			MyUtilities.getMyLibraryFormXMLFile(fileName);
    		assertEquals("Test", endMyLibrary.getName());
    		assertEquals(2, endMyLibrary.getBooks().size());
    		assertEquals(2, endMyLibrary.getPeople().size());
    		assertEquals("Fred", endMyLibrary.getBooks().get(0).getPerson().getName());
    	}
    	
    }
    And in case the MyUtilities class is useful...

    Java Code:
    package org.persistence.tutorial;
    
    import java.io.*;
    
    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.io.xml.DomDriver;
    
    
    public class MyUtilities {
    
    	public static boolean saveStringToFile(String fileName, 
    			String saveString) {
    		boolean saved = false;
    		BufferedWriter bw = null;
    		
    		try {
    			bw = new BufferedWriter(
    					new FileWriter(fileName));
    			try {
    				bw.write(saveString);
    				saved = true;
    			}
    			finally {
    				bw.close();
    			}
    		}
    		catch (IOException ex){
    			ex.printStackTrace();
    		}
    		return saved;
    		
    	}
    	
    	
    	
    
    	public static String getStringFromFile(String fileName) {
    		BufferedReader br = null;
    		StringBuilder sb = new StringBuilder();
    	
    		try {
    			br = new BufferedReader(
    					new FileReader(fileName));
    			try {
    				String s;
    				while ((s = br.readLine()) != null) {
    					// add line feed back since stripped by readline()
    					sb.append(s);
    					sb.append("\n");
    				}
    			}
    			finally {
    				br.close();
    				
    			}
    		}
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    		return sb.toString();
    	}
    
    
    
    
    	public static String convertToXML(MyLibrary ml) {
    		XStream xstream = new XStream(new DomDriver());
    		xstream.setMode(XStream.ID_REFERENCES);
    		xstream.alias("person", Person.class);
    		xstream.alias("book", Book.class);
    		xstream.alias("mylibrary", MyLibrary.class);
    		return xstream.toXML(ml);
    		
    	}
    
    
    
    
    	public static MyLibrary convertFromXML(String XMLString) {
    		MyLibrary ml = null;
    		XStream xstream = new XStream(new DomDriver());
    		xstream.setMode(XStream.ID_REFERENCES);
    		xstream.alias("person", Person.class);
    		xstream.alias("book", Book.class);
    		xstream.alias("mylibrary", MyLibrary.class);
    		Object obj = xstream.fromXML(XMLString);
    		if (obj instanceof MyLibrary){
    			ml= (MyLibrary) obj;
    			
    		}
    		return ml;
    	}
    
    
    
    
    	public static boolean saveMyLibraryToXMLFile(String fileName,
    			MyLibrary ml) {
    		return saveStringToFile(fileName, convertToXML(ml));
    	}
    
    	public static MyLibrary getMyLibraryFormXMLFile(String fileName) {
    		return convertFromXML(getStringFromFile(fileName));
    	}
    
    }
    Any guidance here is appreciated.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Print out the two strings and see?
    Or even simply debug through it?

Similar Threads

  1. Connection failure due to proxy problems
    By thalupularavi in forum Networking
    Replies: 2
    Last Post: 03-10-2010, 02:52 PM
  2. ECLIPSE + MYSQL Communications link failure due to underlying exception
    By transistor47 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-16-2009, 11:21 PM
  3. SQLException: Communications link failure
    By etherkye in forum JDBC
    Replies: 0
    Last Post: 07-03-2009, 04:20 PM
  4. Connect to HTTPS + SSL handshake failure
    By jamojo in forum Advanced Java
    Replies: 3
    Last Post: 06-25-2009, 11:04 PM
  5. Animation failure...
    By lordbob75 in forum New To Java
    Replies: 10
    Last Post: 05-11-2009, 02:06 AM

Tags for this Thread

Posting Permissions

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