简体   繁体   English

测试Spring Boot应用程序?

[英]Testing a Spring Boot application?

I want to create a Spring Boot test for a Controller-class. 我想为控制器类创建一个Spring Boot测试。

The method I want to test is: 我要测试的方法是:

private String statusQueryToken;

@RequestMapping("/onCompletion")
public String whenSigningComplete(@RequestParam("status_query_token") String token){
    this.statusQueryToken = token;

I am unsure about how to test things in Spring Boot. 我不确定如何在Spring Boot中测试事物。

If I wanted to test that the field statusQueryToken has been initialized with the @RequestParam("status_query_token") , how would I go about doing this? 如果我想测试状态statusQueryToken是否已使用@RequestParam("status_query_token")进行了初始化,我该怎么做?

Thank you! 谢谢!

You can use Spring MockMvc 您可以使用Spring MockMvc

So try something like this: 所以尝试这样的事情:

MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(get("/onCompletion").param("status_query_token", "yourToken"))
                .andExpect(status().isOk());

There are several way you can approach this. 有几种方法可以解决此问题。 My preferred way to test with the real tomcat instance. 我首选的使用实际tomcat实例进行测试的方法。

    @RunWith(SpringJUnit4ClassRunner.class)
    @WebIntegrationTest("server.port:0")
    @SpringApplicationConfiguration(YourMainApplication.class)
    public class TestClass() {
        @Value("${local.server.port}")
        private int port;
    @Autowired
    private RestTemplate restTemplate;

   public <T> T get(String path, Class<T> responseType, Object... pathVariables) {
        return restTemplate.getForEntity(path, responseType, pathVariables).getBody();
    }

    }

There are different ways to test 有不同的测试方法

1. Using Maven 1.使用Maven

$ mvn clean install

This will generate the jar file with spring boot embedded with tomcat by default 默认情况下,这将生成带有嵌入tomcat的spring boot的jar文件

$ mvn spring-boot:run

This will run you spring app 这将运行您的春季应用程序

Now Spring is up and running 现在,Spring已启动并运行

2. Creating an executable jar (in absence on maven) 2.创建一个可执行jar(在Maven上不存在)

$ java -jar target/myproject.0.0.1.SNAPSHOT.jar

"Same effect as above" “与上述效果相同”

Now open the browser or soap ui or fiddler or postmaster to send request to the controller 现在打开浏览器或soap ui或fiddler或postmaster将请求发送到控制器

ex: GET method http://localhost:8080/myproject/onCompletion/hello 例如:GET方法http:// localhost:8080 / myproject / onCompletion / hello

http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-first-application.html

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = ApplicationConfiguration.class)
public class ItemFeedTest {

@InjectMocks
private PersonRepository personRepository;


@Autowired
@Mock
private PersonService personService;


@Before
    public void setup() throws IOException {
        MockitoAnnotations.initMocks(this);
    }

@Test
    public void onSuccessTest() {
        //Write the Junit Test and mock the required Class
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM