今天在写一个测试代码的时候, junit突然报了这个错误, 百度了一下, 各种说法都有, 但是跟我的都不匹配
private SqlSessionFactory factory = null;
@Before
private void init() throws IOException {
// 1.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 2.加载配置文件
InputStream is = Resources.getResourceAsStream("Configuration.xml");
// 3.创建SqlSessionFactory对象
factory = builder.build(is);
}
@Test
public void test1() throws IOException {
// 4.创建SqlSession对象
SqlSession session = factory.openSession();
// 5.使用SqlSession对象执行查询,得到结果
Book book = session.selectOne("findBookById", "a000Mm1WSnpoSjFIMVF4VE1LWEpKUT09");
// 6.显示结果
System.out.println(book);
// 7.释放资源
session.close();
}
报的错误:
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test1], {ExactMatcher:fDisplayName=test1(com.gongxm.test.MyTest)], {LeadingIdentifierMatcher:fClassName=com.gongxm.test.MyTest,fLeadingIdentifier=test1]] from org.junit.internal.requests.ClassRequest@946e09
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
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:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
后来发现, 原来是有一个测试方法写成了私有的:
@Before
private void init() throws IOException {
// 1.创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 2.加载配置文件
InputStream is = Resources.getResourceAsStream("Configuration.xml");
// 3.创建SqlSessionFactory对象
factory = builder.build(is);
}
把这个方法改成公共的之后问题就解决了!
记录一下, 也为后人提供一下参考!
网友评论