Search code examples
javaunit-testingtestingjunit

Unable to run any Junit4 tests - InvalidTestClassError: Invalid test class


I am trying to run Junit4 tests and I am unable to run it. I have the following dependency installed

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>

My test class looks like this

package model.validators;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

class UserValidatorTest {

    @Test
    public void shouldReturnTrueForValidPhoneNumbers() {
        List<String> phoneNumbers = Arrays.asList(
                "9876543210",
                "7777543210"
        );
        boolean result = UserValidator.validateUserPhoneNumbers(phoneNumbers);

        Assert.assertTrue(result);
    }
}

When I try to run this test, I get the following error

org.junit.runners.model.InvalidTestClassError: Invalid test class 'model.validators.UserValidatorTest':

I am using IntellijIdea. Any idea what is going wrong here ? TIA

Tried changing dependencies, reloading maven project, setting the correct classpath in Junit Run configurations


Solution

  • I can see that your class UserValidatorTest is not public. On making your class public, you will be able to run the tests.

    package model.validators;
    
    import org.junit.Assert;
    import org.junit.Test;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class UserValidatorTest {
    
        @Test
        public void shouldReturnTrueForValidPhoneNumbers() {
            List<String> phoneNumbers = Arrays.asList(
                    "9876543210",
                    "7777543210"
            );
            boolean result = UserValidator.validateUserPhoneNumbers(phoneNumbers);
    
            Assert.assertTrue(result);
        }
    }