Revize f0bf4c92
Přidáno uživatelem smoldas15 před téměř 5 roky(ů)
be/fulltextsearch/src/test/java/cz/zcu/kiv/aswi/fulltextsearch/FulltextSearchApplicationTests.java | ||
---|---|---|
1 | 1 |
package cz.zcu.kiv.aswi.fulltextsearch; |
2 | 2 |
|
3 |
import org.apache.jute.Index; |
|
3 | 4 |
import org.junit.jupiter.api.Test; |
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
4 | 6 |
import org.springframework.boot.test.context.SpringBootTest; |
5 | 7 |
|
8 |
import static org.assertj.core.api.Assertions.assertThat; |
|
9 |
|
|
6 | 10 |
@SpringBootTest |
7 | 11 |
class FulltextSearchApplicationTests { |
8 | 12 |
|
... | ... | |
10 | 14 |
void contextLoads() { |
11 | 15 |
} |
12 | 16 |
|
17 |
@Autowired |
|
18 |
private IndexController controller; |
|
19 |
|
|
20 |
@Test |
|
21 |
public void contextLoads2() throws Exception { |
|
22 |
assertThat(controller).isNotNull(); |
|
23 |
} |
|
24 |
|
|
13 | 25 |
} |
be/fulltextsearch/src/test/java/cz/zcu/kiv/aswi/fulltextsearch/HttpRequestTest.java | ||
---|---|---|
1 |
package cz.zcu.kiv.aswi.fulltextsearch; |
|
2 |
|
|
3 |
import org.junit.jupiter.api.Test; |
|
4 |
import org.springframework.beans.factory.annotation.Autowired; |
|
5 |
import org.springframework.boot.test.context.SpringBootTest; |
|
6 |
import org.springframework.boot.test.web.client.TestRestTemplate; |
|
7 |
import org.springframework.boot.web.server.LocalServerPort; |
|
8 |
|
|
9 |
import static org.assertj.core.api.Assertions.assertThat; |
|
10 |
|
|
11 |
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
|
12 |
public class HttpRequestTest { |
|
13 |
|
|
14 |
@LocalServerPort |
|
15 |
private int port; |
|
16 |
|
|
17 |
@Autowired |
|
18 |
private TestRestTemplate restTemplate; |
|
19 |
|
|
20 |
@Autowired |
|
21 |
private IndexController controller; |
|
22 |
|
|
23 |
@Test |
|
24 |
public void infoTest() throws Exception { |
|
25 |
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/info", |
|
26 |
String.class)).contains(controller.info()); |
|
27 |
} |
|
28 |
|
|
29 |
@Test |
|
30 |
public void addTest() throws Exception { |
|
31 |
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/add", |
|
32 |
String.class)).contains(controller.add()); |
|
33 |
} |
|
34 |
} |
be/fulltextsearch/src/test/java/cz/zcu/kiv/aswi/fulltextsearch/WebAppTest.java | ||
---|---|---|
1 |
package cz.zcu.kiv.aswi.fulltextsearch; |
|
2 |
|
|
3 |
import org.junit.jupiter.api.Test; |
|
4 |
import org.springframework.beans.factory.annotation.Autowired; |
|
5 |
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
|
6 |
import org.springframework.boot.test.context.SpringBootTest; |
|
7 |
import org.springframework.test.web.servlet.MockMvc; |
|
8 |
|
|
9 |
import static org.hamcrest.Matchers.containsString; |
|
10 |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
|
11 |
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
|
12 |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
|
13 |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
14 |
|
|
15 |
@SpringBootTest |
|
16 |
@AutoConfigureMockMvc |
|
17 |
public class WebAppTest { |
|
18 |
|
|
19 |
@Autowired |
|
20 |
private MockMvc mockMvc; |
|
21 |
|
|
22 |
@Test |
|
23 |
public void infoTest() throws Exception { |
|
24 |
this.mockMvc.perform(get("/info")).andDo(print()).andExpect(status().isOk()) |
|
25 |
.andExpect(content().string(containsString("SolrServerException"))); |
|
26 |
} |
|
27 |
} |
be/fulltextsearch/src/test/java/cz/zcu/kiv/aswi/fulltextsearch/WebLayerTest.java | ||
---|---|---|
1 |
package cz.zcu.kiv.aswi.fulltextsearch; |
|
2 |
|
|
3 |
import org.junit.jupiter.api.Test; |
|
4 |
import org.mockito.Mockito; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
|
7 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
8 |
import org.springframework.test.web.servlet.MockMvc; |
|
9 |
|
|
10 |
import static org.hamcrest.Matchers.containsString; |
|
11 |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
|
12 |
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
|
13 |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
|
14 |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
15 |
|
|
16 |
@WebMvcTest |
|
17 |
public class WebLayerTest { |
|
18 |
|
|
19 |
@Autowired |
|
20 |
private MockMvc mockMvc; |
|
21 |
|
|
22 |
@Test |
|
23 |
public void infoTest() throws Exception { |
|
24 |
this.mockMvc.perform(get("/info")).andDo(print()).andExpect(status().isOk()) |
|
25 |
.andExpect(content().string(containsString("SolrServerException"))); |
|
26 |
} |
|
27 |
|
|
28 |
/*@MockBean |
|
29 |
private GreetingService service; |
|
30 |
|
|
31 |
@Test |
|
32 |
public void greetingShouldReturnMessageFromService() throws Exception { |
|
33 |
Mockito.when(service.greet()).thenReturn("Hello, Mock"); |
|
34 |
this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk()) |
|
35 |
.andExpect(content().string(containsString("Hello, Mock"))); |
|
36 |
}*/ |
|
37 |
} |
Také k dispozici: Unified diff
Re #7735: Nápsání testů na BE
- základní Spring testy