How to do it...

  1. 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() { } }
  1. In build.gradle, we also get a test dependency on spring-boot-starter-test, as follows:
testCompile("org.springframework.boot:spring-boot-starter-test") 
  1. We will go ahead and extend the basic template test to contain the following code:
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BookPubApplicationTests {
@Autowired
private WebApplicationContext context; @Autowired
private TestRestTemplate restTemplate;
@Autowired
private BookRepository repository;

@LocalServerPort
private int port;

private MockMvc mockMvc; @Before
public void setupMockMvc() {
mockMvc = webAppContextSetup(context).build();
}

@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());
}

@Test
public void webappPublisherApi() throws Exception {
mockMvc.perform(get("/publishers/1")).
andExpect(status().isOk()).andExpect(content().
contentType(MediaType.parseMediaType
("application/hal+json;charset=UTF-8"))). andExpect(content().
string(containsString("Packt"))).
andExpect(jsonPath("$.name").value("Packt"));
}
}
  1. Execute the tests by running ./gradlew clean test.
  2. 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
:test
2016-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 hierarchy
2016-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 export
2016-10-13 21:40:44.780  INFO 25739 --- [       Thread-4] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
    
BUILD SUCCESSFUL
Total time: 24.635 secs
  1. 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:
  2. 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:
  3. 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.199.88