I have a Spring Boot web application that I launch by running this class ...
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } The web app has a JSP/HTML front end served up by Spring MVC Controllers which talk to Services which talk to DAOs which uses Hibernate to read/write Entities to a MySQL database.
All the components and services get instantiated and @Autowired and the web app runs fine.
Now, I want to build JUnit tests and test some of the functionality in the Services or the DAOs.
I started writing a JUnit test like below, but I quickly got stuck on not knowing how to instantiate all the @Autowired components and classes.
public class MySQLTests { @Test public void test000() { assertEquals("Here is a test for addition", 10, (7+3)); } @Autowired UserService userService = null; @Test public void test001() { userService.doSomething("abc123"); // ... } } I basically want the web application to start up and run, and then have the JUnit tests run the methods in those Services.
I need some help getting started ... is there some kind of JUnit equivalent of the @SpringBootApplication annotation that I can use in my JUnit class?
@SpringBootTest. Worth reading this about junits for spring-boot apps.public class SmokeTest, I added @SpringBootTest and @Autowired and a simple null check, but my autowired service is not getting instantiated. Any idea why?@RunWith(SpringRunner.class)and a couple other details. I'll write up my solution as an answer. Thanks for the help!