Search code examples
javajunit

My JUnit tests don't fail when I execute them via maven


I use maven on my test project and I wanted to test test option in maven's lifecycle, but I don't get my JUnit test failed. I have class named Arithmetics in src.main.java package and class named ArithmeticsTest in src.test.java package. When I run ArithmeticsTest on my own using IntelliJ Idea everuthing OK, and I have expected java.lang.AssertionError, so why I don't have such when I run test option in maven?

Console output:


T E S T S


Running ArithmeticsTest Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@44e81672 Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.438 sec Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

src.main.java.Arithmetics.java

public class Arithmetics
{
    public static int add(int a, int b)
    {
        return a + b;
    }
}

src.test.java.ArithmeticsTest.java

import org.junit.Test;
import static org.junit.Assert.*;

public class ArithmeticsTest
{
    @Test
    public void testAdd()
    {
        assertEquals(4, Arithmetics.add(2, 3));
    }
}

Solution

  • You have configured maven to use TestNG but you are using JUnit annotations (org.junit.Test) and assertions (org.junit.Assert.*).

    Remove TestNG from your dependencies and it should work.