Hussain Ashruf
Hussain Ashruf

Reputation: 441

Executing JUnit test case for Java standalone that takes command line inputs

I have wrote a Java standalone application that takes input command line arguments. How to test it using JUnit test cases?

Upvotes: 2

Views: 1976

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340708

Just call your main() method from JUnit. However, since main() does not return anything, you can't easily write any assertions (you must depend on side effects).

So for the sake of simplicity and maintainability of your tests, separate main() method with parameter parsing from the actual logic. Test main() alone with mocked logic to test parameter parsing and test business logic separately. Your business logic will have well defined, strongly typed interfaces, making it much easier to test and understand.

Also this step will reduce coupling and increase readability.

Upvotes: 3

Related Questions