brauche HILFE beim Junit test:eek:

tanzverfuehrung

Bekanntes Mitglied
😱
ich soll einen Junit test für die methode
boolean nodeExists(String pathName) throws BackingStoreException
machen


Returns true if the named node exists. Accepts a relative or absolute pathname. Absolute pathnames (which begin with '/') are interpreted relative to the root of this node. Relative pathnames (which begin with any character other than '/') are interpreted relative to this node itself. The pathname "" is valid, and refers to this node itself.

If this node (or an ancestor) has already been removed with the removeNode() method, it is legal to invoke this method, but only with the pathname ""; the invocation will return false. Thus, the idiom p.nodeExists("") may be used to test whether p has been removed.

Parameters:
pathName the path name of the node whose existence is to be checked.
Returns:
true if the specified node exists.
Throws:
BackingStoreException - if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
IllegalStateException - if this node (or an ancestor) has been removed with the removeNode() method and pathname is not the empty string ("").
IllegalArgumentException - if the path name is invalid (i.e., it contains multiple consecutive slash characters, or ends with a slash character and is more than one character long).



ich habe erstmal zwei tests geschrieben und würde gern wissen,ob die richtig sind!?!
kann mir jemand helfen!?
Java:
	@Test
	public void testNodesExistsRelativePath() throws Exception {
		Preferences a = simple.node("a/b/c");
		boolean nodeExists = a.nodeExists("b/c");
		assertTrue(nodeExists);
	}
	@Test
	public void testNodeExistsRelativePath() throws Exception {
		Preferences a = simple.node("a/b/c");
		boolean nodeExists = a.nodeExists("b");
		assertTrue(nodeExists);
	}
 
Die 2 Tests sehen ok aus, der text hat aber weitere Anforderungen, die von den tests nicht geprüft werden 😉

Was zB. fehlt, sind die tests ob die Exceptions geworfen werden, auch fehlt der Test, ob "" true liefert bei einer Node die nicht removed wurde und ob false zurückgeliefert wird falls sie schon removed wurde, und natürlich ein test der zeigt das im Falle eines falschen Pfades ein false zurückgegeben wird, etc. pp.
 
Die 2 Tests sehen ok aus, der text hat aber weitere Anforderungen, die von den tests nicht geprüft werden 😉

Was zB. fehlt, sind die tests ob die Exceptions geworfen werden, auch fehlt der Test, ob "" true liefert bei einer Node die nicht removed wurde und ob false zurückgeliefert wird falls sie schon removed wurde, und natürlich ein test der zeigt das im Falle eines falschen Pfades ein false zurückgegeben wird, etc. pp.

ja ok ich weiß das ich noch nicht alle fälle getestet habe,aber wollt erstmal wissen,ob die richtig sind!
DANKE!:toll:
 
Die 2 Tests sehen ok aus, der text hat aber weitere Anforderungen, die von den tests nicht geprüft werden 😉

auch fehlt der Test, ob "" true liefert bei einer Node die nicht removed wurde und ob false zurückgeliefert wird falls sie schon removed wurde


und wie gehe ich diesen fall am besten an?!
da könnte ich ebstimmt hilfe gebrauchen!

ich ahbe es jetzt so geschrieben:
Java:
	@Test
	public void testNodeExistsRelativePathWithNotRemove() throws Exception {
		Preferences a = simple.node("a/b/c");
		boolean nodeExists = a.nodeExists("");
		assertTrue(nodeExists);
	}
	@Test
	public void testNodeExistsRelativePathWithRemove() throws Exception {
		Preferences a = simple.node("a/b/c");
		a.remove("c");
		boolean nodeExists = a.nodeExists("");
		assertTrue(nodeExists);
	}
 
Sieht doch ok aus, bis auf die tatsache, dass es nicht klar ist, was "simple" ist, aber das steht wohl irgendwo in deinem Code 😉
Würde die temp variable nodeExists weglassen, und direkt im assert die Methode aufrufen.

Du kannst bei der @Test Annotation auch die erwarteten Exceptions eintragen, für die anderen Fälle.
 
Sieht doch ok aus, bis auf die tatsache, dass es nicht klar ist, was "simple" ist, aber das steht wohl irgendwo in deinem Code 😉
Du kannst bei der @Test Annotation auch die erwarteten Exceptions eintragen, für die anderen Fälle.

ja genau
simple = new SimplePreferences("root", null);
Du kannst bei der @Test Annotation auch die erwarteten Exceptions eintragen, für die anderen Fälle.


meinst du das so?
Java:
@Test
	public void testNodeExistsRelativePathWithNotRemove() throws IllegalStateException {
		Preferences a = simple.node("a/b/c");
		boolean nodeExists = false;
		try {
			nodeExists = a.nodeExists("");
		} catch (BackingStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertTrue(nodeExists);
	}


oder meinst du so???
Java:
@Test
	public void testNodeExistsRelativePathWithNotRemove() throws IllegalStateException, BackingStoreException {
		Preferences a = simple.node("a/b/c");
		boolean nodeExists = a.nodeExists("");
		assertTrue(nodeExists);
	}


oder muss ich das einzeln testen!?
und kann ich die BackingStoreException bei allen test hinzufügen?!
 
Ich meinte damit sowas:
Java:
@Test(expected=IllegalArgumentException.class)
public void testBla() throws Exception {
// code der eine IllegalStateException verursacht
}
Try/catch und throws BackingStoreException machen keinen Sinn in Tests.
Der Tests läuft durch, und wenn nicht dann ist er fehlgeschlagen, nix Exception handling 😉
 
Ich meinte damit sowas:
Java:
@Test(expected=IllegalArgumentException.class)
public void testBla() throws Exception {
// code der eine IllegalStateException verursacht
}
Try/catch und throws BackingStoreException machen keinen Sinn in Tests.
Der Tests läuft durch, und wenn nicht dann ist er fehlgeschlagen, nix Exception handling 😉

achso also mal wieder ganz falsch gedacht😱

also so z.b?
Java:
	}
	@Test(expected=IllegalStateException.class)
	public void testNodeExistsRelativePathWithRemove() throws Exception  {
		Preferences a = simple.node("a/b/c");
		a.remove("c");
		boolean nodeExists = a.nodeExists("");
		assertTrue(nodeExists);
	}
	@Test(expected=IllegalArgumentException.class)
	public void testIllegalArgumentException() throws Exception {
		Preferences a = simple.node("a//bc//d/");
		assertFalse(a.nodeExists("d"));
		}


und bei der BackingStoreException habe ich nicht mal ne idee wie ich sie prüfen kann???:L
kannst mir nen tipp geben?!?!
 
Zuletzt bearbeitet:
Das müsste imho so aussehene:
Java:
    @Test(expected=IllegalStateException.class)
    public void testNodeExistsRelativePathWithRemove() throws Exception  {
        Preferences a = simple.node("a/b/c");
        a.remove("c");
        a.nodeExists("irgendetwas");
    }
Die Exception soll ja geworfen werden, ein assert der nie erreicht wird (weil ja vorher die Exception fliegt) gehört raus.

Eine backingstoreException kannst du wohl nciht in einem isolierten Unittest erzeugen, ausser du machst absichtlich Fehler beim initialisieren der Preferences.
 
ich wäre bei den expected=xxxx immer vorsichtig.

man hat somit keine Kontrolle, ob die Exception an der erwartenden Stelle auch geflogen ist. wenn sie an einer anderen Stelle geflogen ist hat man einen scheinbar grünen Test und somit noch zusätzlich ein wahres Problem verschleiert
 
Eine backingstoreException kannst du wohl nciht in einem isolierten Unittest erzeugen, ausser du machst absichtlich Fehler beim initialisieren der Preferences.



also kann ich nur 2 von 3 exceptions prüfen???

und so ist der junit test code jetzt richtig oder?
Java:
	 @Test(expected=IllegalStateException.class)
	    public void testIllegalStateException() throws Exception  {
	        Preferences a = simple.node("a/b/c");
	        a.remove("c");
	        a.nodeExists("irgendetwas");
	    } 
	
	@Test(expected=IllegalArgumentException.class)
	public void testIllegalArgumentException() throws Exception {
		Preferences a = simple.node("a//bc//d/");
a.nodeExists("bc");
	}
???:L:bahnhof:
 

Neue Themen


Zurück
Oben