测试类应该只有一个公共零参数构造函数
2022-09-02 00:43:15
我写了一个测试类,如下所示
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;
}
}
我的 BaseRepositoryTest 类紧随其后
@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);
}
}
当我运行我的测试类时,它显示如下:
Test class should have exactly one public zero-argument constructor:at
org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor
我想用@parameters做一个测试方法,所以请任何人都可以帮忙找到解决方案