SpringBoot: how to test if the service is up and running using rest-assured - spring-boot

In my spring boot application how can I test if the service is up and running using rest assured. the main() is defined like this -
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

Your test should look like this
public class ApplicationTest {
#Before
public void setup() {
RestAssured.baseURI = "http://localhost:8080";
}
#Test
public void testStatus() {
given().contentType(ContentType.JSON).get("/greeting").prettyPeek().then().statusCode(200);
}
#Test
public void testMessage() {
given().contentType(ContentType.JSON).get("/greeting").then()
.body("content", is("Hello, World!"));
}
}
Include dependency below in your build tool:
Gradle:
testCompile('com.jayway.restassured:rest-assured:2.4.1')
Maven:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.4.1</version>
</dependency>

Related

spring boot "#ComponentScan(nameGenerator = CustomGenerator.class)" not working jpaRepository bean

#ComponentScan(nameGenerator = CustomGenerator.class)
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
if you do it with the above code, the name of jpa Repository Bean will not be changed
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.beanNameGenerator(new CustomGenerator())
.sources(SampleApplication.class)
.run();
}
}
If the code is used above, the name of the jpa Repository bean will be changed, but the CustomGenerator will not be injected during the test
what should i do!! helppppp
it is the same as above!!

Spring Boot - connect resource server to Spring Session with an Redis server

For evaluation purposes we would like to use Spring Session in combination with an Embedded-Redis server.
When using Spring Boot 2.6.7 with JDK17 we start the Spring Session with an Embedded-Redis server. The server is started just after the 'UI' server. The code and config is given at the end.
The 'resource' server that is receiving the Spring Session token cannot connect to redis.
The resource server code is:
#SpringBootApplication
#RestController
public class ResourceApplication extends WebSecurityConfigurerAdapter {
private final static Logger logger = LoggerFactory.getLogger(ResourceApplication.class);
#Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().authorizeRequests().anyRequest().authenticated();
}
#RequestMapping("/")
#CrossOrigin(origins = "*", maxAge = 3600, allowedHeaders = { "x-auth-token", "x-requested-with", "x-xsrf-token" })
public Message home() {
logger.info( "ResourceServer: home()");
return new Message("Hello World");
}
#Bean
HeaderHttpSessionStrategy sessionStrategy() {
return new HeaderHttpSessionStrategy();
}
public static void main(String[] args) {
SpringApplication.run(ResourceApplication.class, args);
}
}
The properties are:
security.sessions=NEVER
spring.session.store-type=redis
spring.redis.host=localhost
spring.redis.port=6379
The dependencies are:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
When adding e.g. the spring-session-data-redis
The error message is:
An attempt was made to call a method that does not exist. The attempt
was made from the following location:
org.springframework.boot.autoconfigure.session.RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration.customize(RedisSessionConfiguration.java:80)
When adding the spring-sessions-data-redis dependency, gives the same error message.
Only for your information, starting up the Redis-embedded server goes like this:
#SpringBootApplication
#EnableWebSecurity
public class DemoApplication {
private Logger logger = LoggerFactory.getLogger(getClass());
private RedisServer redisServer;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#PostConstruct
public void startRedis() throws IOException {
try {
redisServer = RedisServer.builder().setting("maxheap 256Mb").port(6379).build();
redisServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
#PreDestroy
public void stopRedis(){
redisServer.stop();
}
}
Dependencies:
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.3</version>
</dependency>
The resources:
spring.redis.host=localhost
spring.redis.password=
spring.redis.port=6379
spring.session.store-type=redis

spring boot postconstruct not called when running java -jar

I have the following class which has postconstruct.. But it doesnt seem to call the postconstruct? But it works in the eclipse IDE? But when i call it via java -jar... nothing
#SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#PostConstruct
public void postcontruct() {
System.out.println("******************TEST ********");
}
}

ApplicationReadyEvent not listened to when declaring #ComponentScan for library package

#componentscan({"com.test.cloud"})
#SpringBootApplication
public class TestClass {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(TestClass.class, args);
}
}
public class TestClass2{
#eventlistener(ApplicationReadyEvent.class)
public void testMethod() {
.....
.....
}
}
Here if I remove #componentscan({"com.test.cloud"}) annotation then the testMethod is triggered. But when I use #componentscan({"com.test.cloud"}) then the testMethod is not triggered.
SpringBoot Version: 1.5.10.RELEASE
Worked by adding:
#componentscan({"com.test.cloud","com.example.demo"})

How to write groovy test for main class of Spring-boot application thast uses CommandLineRunner

My class is like:
#SpringBootApplication
class Test implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
#Override
public void run(String... args) throws Exception {
//some code
}
}
How do I pass argument to run method? Currently written test as:
def "loads for class"() {
expect:
true
}
but it can't find the args for run method and fails.
If you use classic spring integration test, you can use:
#SpringBootTest(args = {"YOUR", "ARGS", "GO", "HERE"})

Resources