Error executing an assert in eclipse

Asked

Viewed 358 times

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 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 someone help me with this?

  • NoClassDefFoundError indicates that Junit is not in the classpath of your project. You are sure that it is there?

  • How are you importing Junit? Via Maven or jar import? Please explain how your project is structured. At.

  • It’s a Maven project, all classes are in the main package. To import I just added these lines in the pom: <dependency> <groupid>junit</groupid> <artifactId>junit</artifactId> <version>4.10</version> <Scope>test</Scope> </dependency>

1 answer

2


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

<scope>test</scope>`

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

However, the method main 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, ams has to be within that directory. So 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 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 testing here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.