Error executing an assert in eclipse

0

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 do I run 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 this?

    
asked by anonymous 11.08.2014 / 21:31

1 answer

2

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

<scope>test</scope>'

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

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

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

Example:

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

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

Read a little more about unit testing here .

    
12.08.2014 / 21:16