Skip to content
Permalink
Browse files

How to test a REST facade

  • Loading branch information...
slem1 committed Oct 27, 2017
1 parent 2860d42 commit 4fc82d4b7f0b8385f0a97b534d13c798577e39a4
@@ -18,6 +18,11 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
@@ -0,0 +1,21 @@
package fr.sle.testhowto.rest.facade;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author slemoine
*/
@RestController
@RequestMapping(MyRestFacade.ROOT_RESOURCE)
public class MyRestFacade {

public static final String ROOT_RESOURCE = "/myrestfacade";

@GetMapping
public String getAString(){
return "Hello !";
}

}
@@ -0,0 +1,38 @@
package fr.sle.testhowto.test;

import fr.sle.testhowto.rest.facade.MyRestFacade;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

/**
* @author slemoine
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyRestFacadeTest {

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Before
public void before(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void getAStringTest() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(MyRestFacade.ROOT_RESOURCE))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
@@ -9,12 +9,11 @@
* @author slemoine
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootTest()
public class MyTestClass {

@Test
public void aTestShouldAssertSomething(){

public void aTestShouldAssertSomething() {
}

}

0 comments on commit 4fc82d4

Please sign in to comment.
You can’t perform that action at this time.