Spring Boot gets us going by creating a placeholder test file, BookPubApplicationTests.java, in the src/test/java/com/example/bookpub directory at the root of our project with the following content:
@RunWith(SpringRunner.class)
@SpringApplicationConfiguration(classes = BookPubApplication.class)
public class BookPubApplicationTests {
@Test
public void contextLoads() {
}
}
In build.gradle, we also get a test dependency on spring-boot-starter-test, as follows:
@Test public void contextLoads() { assertEquals(1, repository.count()); }
@Test public void webappBookIsbnApi() { Book book = restTemplate.getForObject("http://localhost:" + port + "/books/978-1-78528-415-1", Book.class); assertNotNull(book); assertEquals("Packt", book.getPublisher().getName()); }
Execute the tests by running ./gradlew clean test.
By looking at the console output, we can tell that our tests have succeeded and are running, but we don't really see much information besides the following lines (truncated for brevity):
:compileJava:compileTestJava:testClasses:test2016-10-13 21:40:44.694 INFO 25739 --- [ Thread-4] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@206f4aa6: startup date [Mon Apr 13 21:40:36 CDT 2015]; root of context hierarchy2016-10-13 21:40:44.704 INFO 25739 --- [ Thread-4] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'2016-10-13 21:40:44.705 INFO 25739 --- [ Thread-4] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export2016-10-13 21:40:44.780 INFO 25739 --- [ Thread-4] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export completeBUILD SUCCESSFULTotal time: 24.635 secs
Better insight can be gathered by viewing the HTML reports that are generated by Gradle, which can be opened in the browser and reside in build/reports/tests/index.html, as shown in the following screenshot:
Clicking on com.example.bookpub.BookPubApplicationTests will take us to the individual test case breakdown, which shows the status of each test and how long it took to get executed, as follows:
The more curious minds can also click on the Standard output button in order to see the runtime application logs that are produced during the execution of the test.