Saturday, May 4, 2019

12 - Spring boot Introduction to Unit Testing with JUnit Hello World



POM.xml

   
<!--xml version="1.0" encoding="UTF-8"?-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>TestingTuto12</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>TestingTuto12</name>
 <description>Demo project for Spring Boot</description>

 <properties>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>

   
  


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



No comments:

Post a Comment