Skip to content

Commit 5d411fb

Browse files
authored
Merge pull request eugenp#505 from chernykhalexander/master
Added Spring Boot Startets module
2 parents 1a812c4 + 5b28a3c commit 5d411fb

10 files changed

Lines changed: 285 additions & 1 deletion

File tree

‎spring-boot/pom.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@
6060
<artifactId>spring-boot-starter-test</artifactId>
6161
<scope>test</scope>
6262
</dependency>
63+
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.jayway.jsonpath</groupId>
70+
<artifactId>json-path</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-starter-mail</artifactId>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.subethamail</groupId>
79+
<artifactId>subethasmtp</artifactId>
80+
<version>3.1.7</version>
81+
<scope>test</scope>
82+
</dependency>
6383
</dependencies>
6484

6585
<build>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.context.ApplicationContext;
5+
6+
@org.springframework.boot.autoconfigure.SpringBootApplication
7+
public class Application {
8+
private static ApplicationContext applicationContext;
9+
10+
public static void main(String[] args) {
11+
applicationContext = SpringApplication.run(Application.class, args);
12+
}
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.baeldung.controller;
2+
3+
import org.baeldung.domain.GenericEntity;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
@RestController
13+
public class GenericEntityController {
14+
private List<GenericEntity> entityList = new ArrayList<>();
15+
16+
{
17+
entityList.add(new GenericEntity(1l, "entity_1"));
18+
entityList.add(new GenericEntity(2l, "entity_2"));
19+
entityList.add(new GenericEntity(3l, "entity_3"));
20+
entityList.add(new GenericEntity(4l, "entity_4"));
21+
}
22+
23+
@RequestMapping("/entity/all")
24+
public List<GenericEntity> findAll() {
25+
return entityList;
26+
}
27+
28+
@RequestMapping(value = "/entity", method = RequestMethod.POST)
29+
public GenericEntity addEntity(GenericEntity entity) {
30+
entityList.add(entity);
31+
return entity;
32+
}
33+
34+
@RequestMapping("/entity/findby/{id}")
35+
public GenericEntity findById(@PathVariable Long id) {
36+
return entityList.stream().filter(entity -> entity.getId().equals(id)).findFirst().get();
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.baeldung.domain;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class GenericEntity {
10+
@Id
11+
@GeneratedValue(strategy = GenerationType.AUTO)
12+
private Long id;
13+
private String value;
14+
15+
public GenericEntity() {
16+
}
17+
18+
public GenericEntity(String value) {
19+
this.value = value;
20+
}
21+
22+
public GenericEntity(Long id, String value) {
23+
this.id = id;
24+
this.value = value;
25+
}
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public String getValue() {
36+
return value;
37+
}
38+
39+
public void setValue(String value) {
40+
this.value = value;
41+
}
42+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.baeldung.repository;
2+
3+
import org.baeldung.domain.GenericEntity;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface GenericEntityRepository extends JpaRepository<GenericEntity, Long> {
7+
}

‎spring-boot/src/main/resources/application.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
server.port=8083
1+
server.port=8080
22
server.contextPath=/springbootapp
33
management.port=8081
44
management.address=127.0.0.1
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.baeldung;
2+
3+
import org.baeldung.domain.GenericEntity;
4+
import org.baeldung.repository.GenericEntityRepository;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.SpringApplicationConfiguration;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
import org.springframework.test.context.web.WebAppConfiguration;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
15+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
16+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
17+
import org.springframework.web.context.WebApplicationContext;
18+
19+
import java.nio.charset.Charset;
20+
21+
import static org.hamcrest.Matchers.hasSize;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertNotNull;
24+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
25+
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@SpringApplicationConfiguration(classes = Application.class)
28+
@WebAppConfiguration
29+
public class SpringBootApplicationTest {
30+
@Autowired
31+
private WebApplicationContext webApplicationContext;
32+
private MockMvc mockMvc;
33+
34+
35+
@Before
36+
public void setupMockMvc() {
37+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
38+
.build();
39+
}
40+
41+
@Test
42+
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
43+
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
44+
MediaType.APPLICATION_JSON.getSubtype(),
45+
Charset.forName("utf8"));
46+
47+
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).
48+
andExpect(MockMvcResultMatchers.status().isOk()).
49+
andExpect(MockMvcResultMatchers.content().contentType(contentType)).
50+
andExpect(jsonPath("$", hasSize(4)));
51+
}
52+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.baeldung;
2+
3+
import org.baeldung.domain.GenericEntity;
4+
import org.baeldung.repository.GenericEntityRepository;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertNotNull;
13+
14+
@RunWith(SpringJUnit4ClassRunner.class)
15+
@SpringApplicationConfiguration(classes = Application.class)
16+
public class SpringBootJPATest {
17+
@Autowired
18+
private GenericEntityRepository genericEntityRepository;
19+
20+
@Test
21+
public void givenGenericEntityRepository_whenSaveAndRetreiveEntity_thenOK() {
22+
GenericEntity genericEntity = genericEntityRepository.save(new GenericEntity("test"));
23+
GenericEntity foundedEntity = genericEntityRepository.findOne(genericEntity.getId());
24+
assertNotNull(foundedEntity);
25+
assertEquals(genericEntity.getValue(), foundedEntity.getValue());
26+
}
27+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.baeldung;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.mail.SimpleMailMessage;
10+
import org.springframework.mail.javamail.JavaMailSender;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
import org.springframework.test.context.web.WebAppConfiguration;
13+
import org.springframework.web.context.WebApplicationContext;
14+
import org.subethamail.wiser.Wiser;
15+
import org.subethamail.wiser.WiserMessage;
16+
17+
import javax.mail.MessagingException;
18+
import java.io.IOException;
19+
import java.util.List;
20+
21+
import static org.hamcrest.Matchers.hasSize;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertThat;
24+
25+
@RunWith(SpringJUnit4ClassRunner.class)
26+
@SpringApplicationConfiguration(classes = Application.class)
27+
public class SpringBootMailTest {
28+
@Autowired
29+
private JavaMailSender javaMailSender;
30+
31+
private Wiser wiser;
32+
33+
private String userTo = "user2@localhost";
34+
private String userFrom = "user1@localhost";
35+
private String subject = "Test subject";
36+
private String textMail = "Text subject mail";
37+
38+
@Before
39+
public void setUp() throws Exception {
40+
final int TEST_PORT = 25;
41+
wiser = new Wiser(TEST_PORT);
42+
wiser.start();
43+
}
44+
45+
@After
46+
public void tearDown() throws Exception {
47+
wiser.stop();
48+
}
49+
50+
@Test
51+
public void givenMail_whenSendAndReceived_thenCorrect() throws Exception {
52+
SimpleMailMessage message = composeEmailMessage();
53+
javaMailSender.send(message);
54+
List<WiserMessage> messages = wiser.getMessages();
55+
56+
assertThat(messages, hasSize(1));
57+
WiserMessage wiserMessage = messages.get(0);
58+
assertEquals(userFrom, wiserMessage.getEnvelopeSender());
59+
assertEquals(userTo, wiserMessage.getEnvelopeReceiver());
60+
assertEquals(subject, getSubject(wiserMessage));
61+
assertEquals(textMail, getMessage(wiserMessage));
62+
}
63+
64+
private String getMessage(WiserMessage wiserMessage) throws MessagingException, IOException {
65+
return wiserMessage.getMimeMessage().getContent().toString().trim();
66+
}
67+
68+
private String getSubject(WiserMessage wiserMessage) throws MessagingException {
69+
return wiserMessage.getMimeMessage().getSubject();
70+
}
71+
72+
73+
private SimpleMailMessage composeEmailMessage() {
74+
SimpleMailMessage mailMessage = new SimpleMailMessage();
75+
mailMessage.setTo(userTo);
76+
mailMessage.setReplyTo(userFrom);
77+
mailMessage.setFrom(userFrom);
78+
mailMessage.setSubject(subject);
79+
mailMessage.setText(textMail);
80+
return mailMessage;
81+
}
82+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.mail.host=localhost
2+
spring.mail.port=25
3+
spring.mail.properties.mail.smtp.auth=false

0 commit comments

Comments
 (0)