The class Test must have exactly one public constructor with a null argument.

I wrote a test class like the 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 follows

 @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 how,

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

I want to create a testing method with @ parameters, so please someone can help find a solution

+13
source share
5 answers

I think the problem is that you identified two test participants. One of them is @RunWith (Parameterized. class) and the one that comes with spring, because AbstractJUnit4SpringContextTests defines a @RunWith(SpringJUnit4ClassRunner.class) .

Since Junit can only work with one @RunWith , you can only use @Parameterized or AbstractJUnit4SpringContextTests . If you want to use both parameters, you must use @Parameterized and then follow the same logic as SpringJUnit4ClassRunner .

A simple approach would be to simply use spring 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 a TestExecutionListener is TestExecutionListener . spring is usually a lot more like application context caching, etc. A break method should also be implemented that closes the application context.

+8
source

If you are using a single test, make sure you spell the name of the test class correctly.

The common mistake I make is a class called "Foo" and a test class called

'FooTest'

and run one unit test with

'Foo'

instead of 'FooTest'

If "Foo" has an open constructor with arguments, I get an error:

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

Try removing the constructor from the MyParameterizedClassTest class.

I was getting this error when my test class had an open constructor. After removal, everything worked.

+1
source

I have one answer, it is fixed on my JUnit4 machine -> when u checks for an exception

  org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171) org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:148) 

enter the first exception

 protected void validateZeroArgConstructor(List<Throwable> errors) { if(!this.getTestClass().isANonStaticInnerClass() && this.hasOneConstructor() && this.getTestClass().getOnlyConstructor().getParameterTypes().length != 0) { String gripe = "Test class should have exactly one public zero-argument constructor"; errors.add(new Exception(gripe)); } } 

it will check that your constructor has no parameters or not. Try entering an injector or field field (-> not officially recommended)

0
source

This error occurs when your class is not defined as public. In your case, it is abstract, so it cannot be an instance for propositional testing.

0
source

Source: https://habr.com/ru/post/977532/


All Articles