Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class)
@AutoConfigureMockMvc
@TestPropertySource(properties = { "security.basic.enabled=false" })
@TestPropertySource(properties = {"security.basic.enabled=false"})
public class SpringBootWithServletComponentIntegrationTest {

@Autowired private ServletContext servletContext;
@Autowired
private ServletContext servletContext;

@Test
public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() {
Expand All @@ -42,7 +45,8 @@ public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() {
.contains("echo servlet"));
}

@Autowired private TestRestTemplate restTemplate;
@Autowired
private TestRestTemplate restTemplate;

@Test
public void givenServletFilter_whenGetHello_thenRequestFiltered() {
Expand All @@ -59,7 +63,6 @@ public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() {
}



}


Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class)
@AutoConfigureMockMvc
@TestPropertySource(properties = { "security.basic.enabled=false" })
@TestPropertySource(properties = {"security.basic.enabled=false"})
public class SpringBootWithoutServletComponentIntegrationTest {

@Autowired private ServletContext servletContext;
@Autowired
private ServletContext servletContext;

@Autowired private TestRestTemplate restTemplate;
@Autowired
private TestRestTemplate restTemplate;

@Test
public void givenServletContext_whenAccessAttrs_thenNotFound() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.baeldung.autoconfiguration;

import com.baeldung.autoconfiguration.example.AutoconfigurationApplication;
import com.baeldung.autoconfiguration.example.MyUser;
import com.baeldung.autoconfiguration.example.MyUserRepository;
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.autoconfiguration.example.AutoconfigurationApplication;
import com.baeldung.autoconfiguration.example.MyUser;
import com.baeldung.autoconfiguration.example.MyUserRepository;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = AutoconfigurationApplication.class)
@EnableJpaRepositories(basePackages = { "com.baeldung.autoconfiguration.example" })
@EnableJpaRepositories(basePackages = {"com.baeldung.autoconfiguration.example"})
public class AutoconfigurationIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package com.baeldung.displayallbeans;

import static org.assertj.core.api.BDDAssertions.then;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -23,7 +13,15 @@
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;

import com.baeldung.displayallbeans.Application;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.assertj.core.api.BDDAssertions.then;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Expand All @@ -46,10 +44,10 @@ public class DisplayBeanIntegrationTest {
public void givenRestTemplate_whenAccessServerUrl_thenHttpStatusOK() throws Exception {
ResponseEntity<String> entity = this.testRestTemplate.getForEntity(
"http://localhost:" + this.port + "/displayallbeans", String.class);

then(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
}

@Test
public void givenRestTemplate_whenAccessEndpointUrl_thenHttpStatusOK() throws Exception {
@SuppressWarnings("rawtypes")
Expand All @@ -68,14 +66,14 @@ public void givenRestTemplate_whenAccessEndpointUrl_thenReturnsBeanNames() throw
List<Map<String, Object>> allBeans = (List) ((Map) entity.getBody().get(0)).get("beans");
List<String> beanNamesList = allBeans.stream().map(x -> (String) x.get("bean")).collect(Collectors.toList());

assertThat( beanNamesList, hasItem("fooController"));
assertThat( beanNamesList, hasItem("fooService"));
assertThat(beanNamesList, hasItem("fooController"));
assertThat(beanNamesList, hasItem("fooService"));
}

@Test
public void givenWebApplicationContext_whenAccessGetBeanDefinitionNames_thenReturnsBeanNames() throws Exception {
String[] beanNames = context.getBeanDefinitionNames();

List<String> beanNamesList = Arrays.asList(beanNames);
assertTrue(beanNamesList.contains("fooController"));
assertTrue(beanNamesList.contains("fooService"));
Expand Down
10 changes: 5 additions & 5 deletions spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = { "security.basic.enabled=false" })
@TestPropertySource(properties = {"security.basic.enabled=false"})
public class AppLiveTest {

@Autowired
Expand All @@ -27,15 +27,15 @@ public class AppLiveTest {
@Test
public void getIndex() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Index Page")));
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Index Page")));
}

@Test
public void getLocal() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/local").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("/local")));
.andExpect(status().isOk())
.andExpect(content().string(equalTo("/local")));
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.baeldung.toggle;

import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -16,6 +12,10 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = ToggleApplication.class)
@AutoConfigureMockMvc
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.baeldung.utils;

import com.baeldung.utils.controller.UtilsController;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
Expand All @@ -10,32 +11,29 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


import com.baeldung.utils.controller.UtilsController;

public class UtilsControllerIntegrationTest {

@InjectMocks
@InjectMocks
private UtilsController utilsController;

private MockMvc mockMvc;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController)
.build();
.build();

}

@Test
public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception {
String param = "testparam";
this.mockMvc.perform(
post("/setParam")
.param("param", param)
.sessionAttr("parameter", param))
.andExpect(status().isOk());
String param = "testparam";
this.mockMvc.perform(
post("/setParam")
.param("param", param)
.sessionAttr("parameter", param))
.andExpect(status().isOk());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;

/**
* Test class for MyStompSessionHandler
* @author Kalyan
*
*/
public class MyStompSessionHandlerIntegrationTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public void whenPostEmployee_thenCreateEmployee() throws Exception {
given(service.save(Mockito.anyObject())).willReturn(alex);

mvc.perform(post("/api/employees").contentType(MediaType.APPLICATION_JSON)
.content(JsonUtil.toJson(alex)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name", is("alex")));
.content(JsonUtil.toJson(alex)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name", is("alex")));
verify(service, VerificationModeFactory.times(1)).save(Mockito.anyObject());
reset(service);
}
Expand All @@ -63,11 +63,11 @@ public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Excepti
given(service.getAllEmployees()).willReturn(allEmployees);

mvc.perform(get("/api/employees").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].name", is(alex.getName())))
.andExpect(jsonPath("$[1].name", is(john.getName())))
.andExpect(jsonPath("$[2].name", is(bob.getName())));
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].name", is(alex.getName())))
.andExpect(jsonPath("$[1].name", is(john.getName())))
.andExpect(jsonPath("$[2].name", is(bob.getName())));
verify(service, VerificationModeFactory.times(1)).getAllEmployees();
reset(service);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package org.baeldung.boot.boottest;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@DataJpaTest
public class EmployeeRepositoryIntegrationTest {
Expand Down Expand Up @@ -65,7 +65,7 @@ public void givenSetOfEmployees_whenFindAll_thenReturnAllEmployees() {
List<Employee> allEmployees = employeeRepository.findAll();

assertThat(allEmployees).hasSize(3)
.extracting(Employee::getName)
.containsOnly(alex.getName(), ron.getName(), bob.getName());
.extracting(Employee::getName)
.containsOnly(alex.getName(), ron.getName(), bob.getName());
}
}
Loading