JUnit "main (String[]) in extraction cannot be applied to ()"

JUnit "main (String[]) in extraction cannot be applied to ()"

好的,所以我正在尝试测试一个数据库提取模型,该模型从 API 中提取数据并将其放入数据库中。虽然我知道我不能对它进行很多 JUnit 测试,因为它没有任何价值,但我正在尝试创建 JUnit 测试来检查各种方法是否不为空。

到目前为止我已经知道了

    @Test
    public void testMain() throws Exception {

    assertNotNull(extract.main());
    } 

然而()得到:提取中的main(String[])不能应用于() 这会导致错误,我不能 运行 它。

这是代码的其余部分:

public static void main(String[] args) throws Exception {

    Inserter inserter = new Inserter();
    Downloader downloader = new Downloader();

    // config for testing
    String currentlyTestingType = "SourceType";
    String currentlyTestingId = "SourceID";


    DataSource dataSource = getDataSource("SOURCEConfig.json");
    String response = downloader.getDataFromApi(currentlyTestingId,currentlyTestingType, dataSource);
    JSONObject jsonObject = new JSONObject(response);
    inserter.insertOne(jsonObject, currentlyTestingType , dataSource);
} 

还是我完全找错了树,这整个部分都不能进行 JUnit 测试?

您马上就会遇到几个问题。

首先,main 接受一个字符串数组,但您没有传递任何参数。由于您没有使用程序参数,因此可以传递 null -- main(null).

其次,assertNotNull(Object)取一个对象。 Main 没有 return 类型,所以你会在 assertNotNull(void)

上得到另一个编译错误

注意:从另一个 class 调用任何 class 的主要方法并不常见。


至于如何对这个主要方法进行单元测试,你可能想使用更好的测试框架(EasyMock,PowerMock,Mockito,...)