Spring Boot Test

......maven_install(
artifacts = [
......# test
"org.springframework:spring-test:5.2.13.RELEASE",
"org.springframework.boot:spring-boot-test:2.3.9.RELEASE",
"org.springframework.boot:spring-boot-starter-test:2.3.9.RELEASE",
"org.springframework.boot:spring-boot-test-autoconfigure:2.3.9.RELEASE",
"org.mockito:mockito-all:1.10.19",

],
......
bazel run @unpinned_maven//:pin
......java_library(
name = "test-lib",
srcs = glob(["generated/src/test/java/**/*.java"]),
resources = [
],
deps = [
artifact("org.springframework:spring-beans"),
artifact("org.springframework:spring-test"),
artifact("junit:junit"),
artifact("org.springframework.boot:spring-boot-test"),
artifact("org.springframework.boot:spring-boot-test-autoconfigure"),
artifact("org.springframework.boot:spring-boot-starter-test"),
artifact("org.mockito:mockito-all"),
":hello"
]
)
java_test(
name = "run-test",
size = "small",
test_class = "com.awesome.tom.api.TestAll",
runtime_deps = [
":test-lib",
]
)

Spring Boot test annotations

  • RunWith
@RunWith(SpringRunner.class)
  • WebMvcTest
@WebMvcTest(HelloApiController.class)
  • TestPropertySource
@TestPropertySource(properties = {“my.defaul.hello=hey yah yah”})
package com.awesome.tom.api;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
@RunWith(SpringRunner.class)
@WebMvcTest(HelloApiController.class)
@TestPropertySource(properties = {"my.defaul.hello=hey yah yah"})
public class HelloApiControllerTestForValue {
@Autowired
private MockMvc mockMvc;
@Test
public void showYouHowTestPropertySourceWorks() throws Exception {
this.mockMvc.perform(get("/hello-service/v1/hello"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().json("{\"language\":\"English\",\"Content\":\"hey yah yah\"}"));
}
}
  • MockBean
@MockBean
private HelloService service;
package com.awesome.tom.api;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.boot.test.mock.mockito.MockBean;
@RunWith(SpringRunner.class)
@WebMvcTest(HelloApiController.class)
@TestPropertySource(properties = {
"my.defaul.hello=hey yah yah",
})
public class HelloApiControllerTestForService {
@Autowired
private MockMvc mockMvc;
@MockBean
private HelloService service;
@Test
public void showYouHowMockBeanWorks() throws Exception {
when(service.getHello()).thenReturn("Hello, Mock");
this.mockMvc.perform(get("/hello-service/v1/hello"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().json("{\"language\":\"English\",\"Content\":\"Hello, Mock\"}"));
}
}
// FYI, to get the response body for a post request
// MvcResult mvcResult = this.mockMvc.perform(post("url")
// .contentType(MediaType.APPLICATION_JSON)
// .content("{\"some\":\"json\"}")
// .andDo(print())
// .andExpect(status().isAccepted())
// .andReturn();
// String body = mvcResult.getResponse().getContentAsString();
package com.awesome.tom.api;import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
HelloApiControllerTestForService.class,
HelloApiControllerTestForValue.class
})
public class TestAll {
}

--

--

--

Life is beautiful…

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is a transpiler?

Using Cloud Partner Portal CPP API for managing Virtual Machine offers in Azure Marketplace

Premium Lab: PHP-FPM Underflow RCE

Datacoral’s Event-Driven Orchestration Framework Now Available to Customers

New post on The Gate’s Fanpage.

The foundations of the Agile method | Agilitest blog

Best Java Books You can Read to become Expert Java designer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tom Liu

Tom Liu

Life is beautiful…

More from Medium

The Spring Security

What is a Java Collection?

Basics of API’s

Result of a GET

String Builder VS String Buffer