I write tests on the application, I decided to define them all in one set with the @RunWith annotation (Suite.class), but after the merging, the parameterized test does not pass and displays the error No public static parameters method on class.

How can this problem be solved?

AllTests:

@Suite.SuiteClasses( { ItemDaoTest.class, MySqlDaoFactoryTest.class} ) @RunWith(Suite.class) public class AllTests { } 

ItemDaoTest:

 @RunWith(Parameterized.class) public abstract class ItemDaoTest{ protected abstract ItemDao<Entity> dao(); protected Class<? extends Entity> daoClass; protected Entity dto; public ItemDaoTest(Class<? extends Entity> clazz, Entity dto) { this.daoClass = clazz; this.dto = dto; } @Test public void testGetById() throws Exception { dto = dao().getById(1); Assert.assertNotNull(dto); } @Test public void testGetAll() throws Exception{ List<? extends Entity> list = dao().getAll(); Assert.assertNotNull(list); Assert.assertTrue(list.size()>0); } @Test public void testDelete() throws Exception{ List<? extends Entity> list = dao().getAll(); Assert.assertNotNull(list); int oldSize = list.size(); Assert.assertTrue(oldSize>0); dao().delete(list.get(0)); list = dao().getAll(); Assert.assertNotNull(list); int newSize = list.size(); Assert.assertEquals(1, oldSize-newSize); } } 

MySqlDaoFactoryTest:

 public class MySqlDaoFactoryTest extends ItemDaoTest{ private static final Logger LOG = Logger.getLogger(MySqlDaoFactoryTest.class); public MySqlDaoFactoryTest(Class<? extends Entity> clazz, Entity dto) { super(clazz, dto); } private Connection connection; private ItemDao<? extends Entity> dao; @BeforeClass public static void init() throws DBException{ try { // Create initial context System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory"); System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming"); InitialContext ic = new InitialContext(); ic.createSubcontext("java:"); ic.createSubcontext("java:/comp"); ic.createSubcontext("java:/comp/env"); ic.createSubcontext("java:/comp/env/jdbc"); // Construct DataSource MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource(); ds.setURL("jdbc:mysql://localhost:3306/hospital"); ds.setUser("root"); ds.setPassword(" "); ic.bind("java:/comp/env/jdbc/hospital", ds); } catch (NamingException ex) { LOG.trace(ex); } } @Parameterized.Parameters public static Collection<Object[]> getParameters() { return Arrays.asList(new Object[][]{ {Patient.class, new Patient()} {User.class, new User()}, {Reception.class, new Reception()}, {Task.class, new Task()}, {Procedure.class, new Procedure()} }); } @Before public void setUp() throws DBException, SQLException { DaoFactory factory = new MySqlDaoFactory(); connection = factory.getConnection(); LOG.trace("Obtain connection: "+connection); connection.setAutoCommit(false); dao = factory.getDao(connection, daoClass); LOG.trace("Obtain dao: "+dao); } @After public void tearDown() throws SQLException { connection.rollback(); connection.close(); } @Override public ItemDao<Entity> dao() { return (ItemDao<Entity>) dao; } } 

Stack trace:

 java.lang.Exception: No public static parameters method on class ua.nure.martseniuk.SummaryTask4.db.dao.ItemDaoTest at org.junit.runners.Parameterized.getParametersMethod(Parameterized.java:299) at org.junit.runners.Parameterized.<init>(Parameterized.java:246) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101) at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87) at org.junit.runners.Suite.<init>(Suite.java:102) at org.junit.runners.Suite.<init>(Suite.java:70) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:107) at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

PS: I write tests for tao, I heard that this is a bad idea, but it should be so.

  • Trace throw, please - Senior Pomidor
  • question supplemented - Roman
  • Try inside getParameters() initialize the type Collection<Object[]> params = new ArrayList<>(); fill it up and then return params - Senior Pomidor

1 answer 1

JUnit clearly states what the problem is: ItemDaoTest no public static method in the ItemDaoTest class that provides parameters (i.e., annotated with @Parameters ). The method for getting the parameters is searched in the same class, which is annotated with @Parametrized .

You need to move the getParameters() method to ItemDaoTest . Or, in general, to combine these two classes - if you have a single DAO implementation (that is, there isn’t something like OracleDaoFactoryTest ), then there’s little point in separating.