Test class should have exactly one public zero-argument constructor

no public static parameters method on class
no runnable methods
true false a class must have exactly one constructor
junit at java lang reflect constructor newinstance constructor java 423
junit parameterized test
java lang exception the class is not public
java lang exception in selenium
initializationerror failed java lang exception

I have written a test class, such that is following

public class MyParameterizedClassTest extends BaseRepositoryTest {
    private int multiplierA;
    private int multiplierB;
    public MyParameterizedClassTest(int multiplierA) {
        this.multiplierA = multiplierA;

    }   

    @Parameters
    public static Collection<Object[]> data() { 
        Object[][] data = new Object[][] { { 1 }, { 5 }, { 121 } };
        return Arrays.asList(data);
    }

    @Test
    public void testMultiplyException() {
        assertEquals("Result", multiplierA * multiplierA,multiply(multiplierA));
    }

    public int multiply(int a){
        return a*a;
    }
 }

And My BaseRepositoryTest class is following

@RunWith (Parameterized.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public abstract class BaseRepositoryTest extends
    AbstractJUnit4SpringContextTests {



    @Inject
    SessionFactory sessionFactory;

    private Transaction transaction;

    public Session getSession() {
        Session session;
        try {
            session = sessionFactory.getCurrentSession();
        } catch (SessionException se) {
            session = sessionFactory.openSession();
        }
        return session;
    }

    @Before
    public void setUp() throws Exception {
        transaction = getSession().beginTransaction();
    }

    @After
    public void tearDown() throws Exception {
        if (transaction != null) {
            transaction.rollback();
        }
        getSession().close();
    }

    @Before
    public void baseSetUp() {
        MockitoAnnotations.initMocks(this);
    }
}

When I run my test class it shows like,

 Test class should have exactly one public zero-argument constructor:at 
 org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor

I want to make a test method with @parameters,so please Can anyone please help to find the solution

I think the problem is that you defined two test runners. One by yourself @RunWith (Parameterized. class) and one that comes with spring, because an AbstractJUnit4SpringContextTests defines a @RunWith(SpringJUnit4ClassRunner.class).

Since Junit can only deal with one @RunWith you can only use @Parameterized or AbstractJUnit4SpringContextTests. If you want to use both you have to use @Parameterized and than do the same logic that a SpringJUnit4ClassRunner does on your own.

A simple approach can be to just use spring's org.springframework.test.context.TestContextManager.

@RunWith(Parameterized.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public abstract class BaseRepositoryTest extends AbstractJUnit4SpringContextTests {

    private TestContextManager testContextManager;

    @Before
    public void setUpContext() throws Exception {
        this.testContextManager = new TestContextManager(getClass());
        this.testContextManager.prepareTestInstance(this);
    }

}

But this only ensures that the TestExecutionListeners are invoked. Spring normally does a lot more like application context caching and so on. Also a tear down method should be implemented that closes the application context.

JUnit: Test class should have exactly one public zero-argument , class) , meant that JUnit no longer relied on the constructor with arguments, and instead tried to initialize the test class with a zero-argument constructor. But as the Java documentation says, the Java compiler provides a default constructor to classes when no constructors exist. But as the Java documentation says, the Java compiler provides a default constructor to classes when no constructors exist. In my case, since the test class had just one constructor with arguments, and no explicit default constructor, there is no longer an implicit default constructor, so the class can no longer be constructed by JUnit.

If you're running a single test, make sure that you wrote your test class name correctly

A common mistake that I do is to have a class named 'Foo' and a test class named

'FooTest'

and run a single unit test with

'Foo'

instead of 'FooTest'

If 'Foo' has a public constructor with arguments I get the error:

java.lang.Exception: Test class should have exactly one public zero-argument constructor

java.lang.Exception: Test class should have exactly one public zero , Click here to show all. ParentRunner.validate() has thrown an Exception. JUnit. 0. 5. 7. Test class should have exactly one public zero-argument constructor. This non-default constructor makes it impossible for the testing framework to create an instance of the class. public TestScenario1(TestData testData) {. You need a no-arg constructor: public TestScenario1() { //or have no explicit constructor declared at all.

This error is thrown when your class is not defined as public In your case it is abstract, so it can not be instantiate for testing propouses

Test class should have exactly one public zero-argument constructor, I am testing two pages of an application. As a result I have created two different classes and run the test with the help of a Suite Class. java.lang.Exception: Test class should have exactly one public zero-argument constructor at java.lang.reflect.Constructor.newInstance(Unknown Source) "No Runnable methods" -java.lang.Exception: No runnable methods at java.lang.reflect.Constructor.newInstance(Unknown Source)

Try removing constructor from MyParameterizedClassTest class.

I was getting this error when my test class had a public constructor. After removing, it started working.

Test class should have exactly one public constructor (Testing forum , Test class should have exactly one public constructor. Post by: Will Myers , Ranch Hand. Aug 06, 2009 03:26:55. Hi, I'm having a strange issue when running  Questions: I am getting an exception when I run my JUnit tests. java.lang.Exception: Test class should have exactly one public constructor Below is my code snippet, any ideas? package com.tests; import org.junit.Rule; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; public class AllTests implements TestRule { private int retryCount

JUnit 4 vs 5 (Jupiter)

I had this issue when I imported the wrong Test implementation. while my constructor was using JUnit 5 features, I imported the old org.junit.Test rather than org.junit.jupiter.api.Test.

"Junit Error" Test class should has exactly one public zero-argument , In Some case, this error appears, we suspect that it's a problem with JUnit version​, but it has to be investigated [ERROR] App - 1 Test Failed out  Java.lang.Exception: Test class should have exactly one public constructor. So I can only assume that something else is either stripping out the constructor or adding another in at compile time, but what and why, and is there anything i can do about it?.

BlockJUnit4ClassRunner, 1 java.lang.Exception: Test class should have exactly one public zero-argument constructor 2 at org.junit.runners.BlockJUnit4ClassRunner. If you run a suitecase that include all the classes from package: @RunWith(WildcardPatternSuite.class) @SuiteClasses("*/.class") An exception is thrown for classes that are not tests: java.lang.Exception: Test class should have exactly o

Test.None; 016 import org.junit.internal.runners.model.ReflectiveCallable; 017 gripe = "Test class should have exactly one public zero-argument constructor";  When I add public Login() {} constructor, now I receive the message 'java.lang.IllegalArgumentException: Test class can only have one constructor Any thoughts would be highly appreciated. Kind regards EN

Test class should have exactly one public zero-argument constructor. no public static parameters method on class java.lang.exception: no runnable methods

Comments
  • Looks like the wrong test runner is executing your task. We need to figure out why that is to solve this problem.
  • Are you sure your classes are all correctly compiled. Your exception mentions the BlockJUnit4ClassRunner, but a Parameterized is not a subclass of BlockJUnit4ClassRunner. See junit.sourceforge.net/javadoc/org/junit/runners/…. Seems that your compiled code does not contain @RunWith (Parameterized. class)
  • yes I tried with @RunWith (Parameterized. class),Now it shows java.lang.NullPointerException at com.kgfsl.collections.core.security.BaseRepositoryTest.getSession(BaseRepositoryTest.java:45) at com.kgfsl.collections.core.security.BaseRepositoryTest.setUp(BaseRepositoryTest.java:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  • @Duncan: My answer was surely based on a basic java knowledge : Java Constructors-Note, See the Note. If a class defines an explicit constructor, it no longer has a default constructor to set the state of the objects. If such a class requires a default constructor, its implementation must be provided. Any attempt to call the default constructor will be a compile time error if an explicit default constructor is not provided in such a case.
  • @RenéLink it perfectly works for me rene. thanks for the solution
  • Would be even better in a Rule
  • @AlexisHassler Yes it would be. The problem with a Rule is that you can't easily get the test instance and you need it for the testContextManager.prepareTestInstance(this);
  • Yeah I tried this code but i got error like java.lang.NullPointerException at com.kgfsl.collections.core.security.BaseRepositoryTest.getSession(BaseRepository‌​Test.java:45) at com.kgfsl.collections.core.security.BaseRepositoryTest.setUp(BaseRepositoryTest.‌​java:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  • @MdAslam Did you try @Autowired instead of @Inject?
  • Yes! This was me! Thanks. Needed to add Test to the end of my test invocation command.