0

Before using Spring Boot, I would define a main method inside the class where I want to test some methods and eventually delete if for the final version. Here is what I do:

public class UserDAO {

public int getUserSSN( int userId )
{
     //Connect to database and get stuff and return it 
}

public static void main(String[] args)
{
   int x = new UserDAO().getUserSSN( 12 );
   System.out.println( x );
}

}

This allowed me to quickly test stuff as I develop(as opposed to mocking stuff with JUnit). How can I do the same thing in Spring Boot ?

EDIT: This is not a best practice question - I think it is generally accepted that JUnits and other unit testing/mocking frameworks are necessary ensure good developer-level testing. I just want to know how to do this quickly(as in less typing - not speed of execution).

Pepria
  • 394
  • 1
  • 8
  • 22
  • Make multiple test case classes, and call the ones you want? – Rogue Feb 26 '17 at 00:11
  • I hate picking up other peoples code where they have used random `main()` methods to perform ad-hoc testing instead of applying standard JUnit tests that do not clutter up the code, can be used to quickly test the codebase, provide code coverage and metrics, etc. Improve your development habits and follow best practices. – Mads Hansen Feb 26 '17 at 00:19
  • AFAIK, the best you can do is to tell Spring Boot which of the main classes in your codebase to make the main for the JAR. So your testing methodology would be implemented by editing the POM file, or by creating a separate module / POM for each of your on-the-fly tests. But, fundamentally, Spring Boot is not designed to do what you are trying to do. The best solution is to not do it. Seriously. – Stephen C Feb 26 '17 at 00:27

1 Answers1

0

Correct way to make this is using multiple test methods/classes to test what you need (time of single unit test method evaluation is very short). You dont want to have such code in your production classes (since you might forget to delete them).

Also (assuming that you are newbie) you should read about TDD or BDD, because that's how a lot of devs produce code and has multiple pros (also cons or traps).

Answer to edit: you are supposed to have plenty of test classes already in normal (not some studying, tutorial) project so it's not time consuming to write test. Plus always remeber that if you have to test something, if it looked like important thing to test, you should keep this test because tests are also documentation!

mklimasz
  • 121
  • 1
  • 7