Error when performing an assert in the eclipse



  • I have the following lines:

    import static org.junit.Assert.*;
    import org.openqa.selenium.WebElement;
    

    public class ClasseTeste extends Navegadores {
    public static void verificarTitulo() {
    abrirChrome();
    String titulo = driver.getTitle();
    assertTrue(titulo.contains("google"));
    fecharNavegador();
    }
    }

    So how much I run the main:

    public static void main( String[] args ) {
    verificarTitulo();
    }

    This occurs:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/Assert
    at teste.NovoProjeto.ClasseTeste.verificarTitulo(ClasseTeste.java:11)
    at teste.NovoProjeto.Main.main(Main.java:8)
    Caused by: java.lang.ClassNotFoundException: org.junit.Assert
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

    Can anyone help me with that?



  • The problem probably occurs because your JUnit is as test dependency:

    <scope>test</scope>`
    

    This means that it will only be available when you are running a test, for example by running a command mvn test in the project.

    However, the method main used reveals that you are not running the test really as a test, but rather as a normal program.

    To actually perform unit tests you must create a class in src/test/java. It can be in any package, ams has to be inside that directory. Then create public and non-static methods annotated with @Test.

    Example:

    @Test
    public void testarTitulo() {
        ...    
    } 
    

    Then, to perform the methods of the test classes, you must enter the command mvn test on the console for Maven to run the tests. If you are using an IDE as Eclipse, you can also run the test classes by right-clicking them and accessing the menu Run as > Junit Test.

    Read a little more about unit tests http://www.vogella.com/tutorials/JUnit/article.html .




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2