I am a programmer but I hate grammar. Here you will find what I did, doing , will do ... Scattered pieces of knowledge that I have deleted from my mind to the trash bin through out my boring daily coding life. I will also report some of my failures in life so dear to me not success, because I have always learnt much only when I fail.
Saturday, May 4, 2019
12 - Spring boot Introduction to Unit Testing with JUnit Hello World
POM.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.example TestingTuto12 0.0.1-SNAPSHOT TestingTuto12 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
TestingTuto12Application
package com.example.testingtuto12;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestingTuto12Application {
public static void main(String[] args) {
SpringApplication.run(TestingTuto12Application.class, args);
}
}
GreetingController
package com.example.testingtuto12;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@RequestMapping("/greeting")
public Boolean sendGreeting() {
return true;
}
}
TestingTuto12ApplicationTests
package com.example.testingtuto12;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestingTuto12ApplicationTests {
@Test
public void contextLoads() {
}
@Autowired
public GreetingController greetingController;
@Test
public void testSendGreeting() {
Boolean greeting = greetingController.sendGreeting();
//assertEquals(greeting, "Hello World Dude!");
assertTrue(greeting);
}
}
To run the test, right click the testSendGreeting() method and select run as JUnit test as shown below .
Wednesday, May 1, 2019
11 - Spring boot Consuming a microservice with RestTemplate and Eureka
Part 1 to install Eureka Server and let it discover the Eureka client
application.properties
server.port=8080 spring.application.name=consumer-service
POM.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE com.example Tuto10EurekaClientConsumer 0.0.1-SNAPSHOT Tuto10EurekaClientConsumer Demo project for Spring Boot 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-devtools runtime org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
Tuto10EurekaClientConsumerApplication
package com.example.clientConsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class Tuto10EurekaClientConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(Tuto10EurekaClientConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
ClientConsumerController
package com.example.clientConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ClientConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/consumer")
public String getClientMessage() {
String url = "http://localhost:8081/hello";
String url1 = "http://myclient-service/hello";
String res = restTemplate.getForObject(url1, String.class);
return "Message : "+res;
}
}
This is just a trick, hope it paved your way out.
Subscribe to:
Posts (Atom)