2016-09-16 4 views
0

Die isVanityURL-Methode aktualisiert. Siehe unten für die ursprüngliche Frage und den Code basierend auf Shahids Empfehlung. Sehen Sie sich auch die Pfadklasse an, wie von assylias vorgeschlagen.Junit nicht übergeben, obwohl es sollte

public static boolean isVanityPath(String resourcePath) { 

    String resPath = resourcePath; 

    if (resPath == null) { 
     return false; 
    } else { 

     resPath = resPath.trim(); 

     if (!resPath.equalsIgnoreCase(StringPool.BLANK)) { 

      int len = resPath.length(); 
      String startChar = resPath.substring(0, 1); 

      if (startChar.equals(StringPool.FORWARD_SLASH) && len > 1) { 
       resPath = resPath.substring(1, len--); 

      }else{ 
       return false; 
      } 

      int lastIndexOfSlash = resPath.lastIndexOf(StringPool.FORWARD_SLASH); 
      int slashIndex = resPath.indexOf(StringPool.FORWARD_SLASH); 

      if (slashIndex != -1) 
       return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1; 
      else 
       return true; 

     } else { 
      return false; 
     } 
    } 

Dies ist der aktualisierte JUnit-Test

@Before 
public void setUp() { 

    vu = Mockito.mock(ResourcePathUtil.class); 
} 

@Test 
public void testVanityURLWhenRoot() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/")); 
} 

@Test 
public void testVanityURLWhenNull() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath(null)); 
} 

@Test 
public void testVanityURLWhenValidVanity() { 
    Assert.assertTrue(ResourcePathUtil.isVanityPath("/vanitycode")); 
} 

@Test 
public void testVanityURLWhenValidVanityWithTrailingSlash() { 
    boolean retValue = ResourcePathUtil.isVanityPath("/vanitycode/"); 
    Assert.assertFalse("Returned True", retValue); 

} 

@Test 
public void testVanityURLWhenInvalidVanityWithTrailingSlash() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/invalidwithslash/")); 
} 

@Test 
public void testVanityURLWhenInvalidVanity() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/justinvalid")); 
} 

@Test 
public void testVanityURLWhenBlank() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("")); 
} 

Ich habe die folgende Klasse (ResourcePathUtil) mit einer statischen Methode. Ich möchte es mit einem JUnit (URLTest) testen. Einige der Tests (testVanityURLWhenRoot, testVanityURLWhenValidVanity) scheinen jedoch nicht bestanden zu werden, obwohl dies der Fall sein sollte. Irgendwelche Hinweise darauf, was ich falsch mache?

public class ResourcePathUtil { 

/** 
    * 
    * @param url 
    * @param data 
    * @return result 
    */ 
public static boolean isVanityPath(String resourcePath) { 

    String resPath = resourcePath; 

    if (resPath == null) { 
     return false; 
    } else { 

     resPath = resPath.trim(); 

     if (!resPath.equalsIgnoreCase(StringPool.BLANK)) { 

      int len = resPath.length(); 
      String startChar = resPath.substring(0, 1); 

      if (startChar.equals(StringPool.FORWARD_SLASH)) { 
       resPath = resPath.substring(1, len--); 

      } 

      int lastIndexOfSlash = resPath.lastIndexOf(StringPool.FORWARD_SLASH); 
      int slashIndex = resPath.indexOf(StringPool.FORWARD_SLASH); 

      if (slashIndex != -1) 
       return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1; 
      else 
       return true; 

     } else { 
      return false; 
     } 
    } 
} 
} 

Die JUnit-Klasse ist unter

import junit.framework.Assert; 

import org.mockito.Mockito; 
import org.junit.Before; 
import org.junit.Test; 

public class URLTest { 


@Before 
public void setUp() { 

    vu = Mockito.mock(ResourcePathUtil.class); 
} 

@Test 
public void testVanityURLWhenRoot() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/")); 
} 

@Test 
public void testVanityURLWhenNull() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath(null)); 
} 

@Test 
public void testVanityURLWhenValidVanity() { 
    Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode")); 
} 

@Test 
public void testVanityURLWhenValidVanityWithTrailingSlash() { 
    boolean retValue = ResourcePathUtil.isVanityPath("/vanitycode/"); 
    Assert.assertTrue("Returned False", !retValue); 

} 

@Test 
public void testVanityURLWhenInvalidVanityWithTrailingSlash() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/invalidwithslash/")); 
} 

@Test 
public void testVanityURLWhenInvalidVanity() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/justinvalid")); 
} 

@Test 
public void testVanityURLWhenBlank() { 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("")); 
} 

} 

String Pool Klasse unter

public class StringPool { 

public static final String BLANK = ""; 
public static final String FORWARD_SLASH = "/"; 

} 
+0

Warum verwenden Sie nicht die 'Path' Klasse? – assylias

+0

Welcher Testfall schlägt fehl? Alle von ihnen? – nasukkin

+0

@assylias, meinst du [dies] (https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html) – Pragrammer

Antwort

0
@Test 
public void testVanityURLWhenRoot() { 
    // expecting isVanityPath() to return false 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/")); 
} 

@Test 
public void testVanityURLWhenValidVanity() { 
    // expecting isVanityPath() to return false 
    Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode")); 
} 

sowohl in testVanityURLWhenRoot und testVanityURLWhenValidVanity, erwarten Sie false. Aber du bekommst true. Der Grund liegt in der Methode isVanityPath(). In den beiden Fällen ist der Wert slashIndex -1. Und Sie geben true zurück, wenn SlashIndex gleich -1 ist. Aus diesem Grund erhalten Sie in beiden Fällen true Ergebnis, obwohl Sie erwarten, false.

if (slashIndex != -1) { 
    return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1; 
} else { // it executes when slashIndex == -1 
    return true; 
} 

Vorschlag:

Statt:

Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode")); 

schreiben:

Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode")); 

Letzteres ist besser lesbar.

+0

Vielen Dank fixed testVanityURLWhenRoot und testVanityURLWhenValidVanity in der isVanityURL-Methode. Jetzt ein Problem mit dem TestVanityURLWhenValidVanityWithTrailingSlash. – Pragrammer

Verwandte Themen