Giả sử bạn có lớp startup thế này:
package com.example.spring_test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringJUnitTestStartup {
public static void main(String[] args) {
SpringApplication.run(SpringJUnitTestStartup.class);
}
}
- Chúng ta cần tạo ra lớp
BaseIntegrationTest
thế này:
package com.example.spring_test.test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.example.spring_test.SpringJUnitTestStartup;
import com.example.spring_test.service.GoodByeService;
import com.example.spring_test.test.BaseIntegrationTest.Initializer;
import com.example.spring_test.test.BaseIntegrationTest.SomeTestConfiguration;
@ExtendWith(SpringExtension.class)
@SpringBootTest(
classes = SpringJUnitTestStartup.class,
webEnvironment = WebEnvironment.DEFINED_PORT
)
@ContextConfiguration(
initializers = Initializer.class,
classes = SomeTestConfiguration.class
)
public class BaseIntegrationTest {
@MockBean
protected GoodByeService goodByeService;
public static class Initializer
implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
// init something
}
}
public static class SomeTestConfiguration {
@Bean
public Object someBean() {
return "someBean";
}
}
}
- Cuối cùng chúng ta sẽ tạo ra lớp
GoodbyeControllerTest
thế này:
package com.example.spring_test.test;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
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.status;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.test.web.servlet.MockMvc;
@AutoConfigureMockMvc
public class GoodbyeControllerTest extends BaseIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void helloAndGoodByeTest() throws Exception {
// given
final String who = "World";
final String goodBye = "I will never say good bye";
when(goodByeService.goodBye(who)).thenReturn(goodBye);
// when
// then
final String expected = "Hello World! and " + goodBye;
mockMvc.perform(get("/hello-and-goodbye?who=World"))
.andExpect(status().isOk())
.andExpect(content().string(expected));
verify(goodByeService, times(1)).goodBye(who);
}
}
Done!!!
Mình thấy ide báo lỗi.
Mình sửa thêm như này thì được.
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
–
Phạm Tiến Đạt
1654210578000