Commit 3d883d8b authored by Narendar Vakiti's avatar Narendar Vakiti

movkito testcases

parent f754fbf6
...@@ -51,6 +51,14 @@ ...@@ -51,6 +51,14 @@
<version>1.7.30</version> <version>1.7.30</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
package com.junit.bean;
public class Product {
private int productId;
private String productName;
private double price;
private int quantity;
public Product() {
}
public Product(int productId, String productName, double price, int quantity) {
super();
this.productId = productId;
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
...@@ -11,6 +11,12 @@ public class EmployeeResource { ...@@ -11,6 +11,12 @@ public class EmployeeResource {
private static final Logger logger = LoggerFactory.getLogger(EmployeeResource.class); private static final Logger logger = LoggerFactory.getLogger(EmployeeResource.class);
/**
* Calculate total salary per annum of each employee
* @param employee
* @return perAnnum
*/
public double calculateYearlySal(Employee employee) { public double calculateYearlySal(Employee employee) {
logger.info("Calculating Yearly Salary"); logger.info("Calculating Yearly Salary");
double perAnnum = 0; double perAnnum = 0;
...@@ -24,7 +30,11 @@ public class EmployeeResource { ...@@ -24,7 +30,11 @@ public class EmployeeResource {
return perAnnum; return perAnnum;
} }
/**
* Calculating appraisal based on salary of each employee
* @param employee
* @return appraisal
*/
public double calculateAppraisal(Employee employee) { public double calculateAppraisal(Employee employee) {
logger.info("Calculating appraisal based on salary"); logger.info("Calculating appraisal based on salary");
double appraisal = 0; double appraisal = 0;
......
package com.junit.resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.junit.bean.Product;
import com.junit.service.ProductService;
@Controller
public class ProductResource {
private static final Logger logger = LoggerFactory.getLogger(ProductResource.class);
@Autowired ProductService productService;
/**
* Get total price of product
* @return totalPrice
*/
public double getPrice() {
double totalPrice = 0;
Product pen = new Product(101, "Pen", 15, 10);
try {
totalPrice = productService.getTotalPrice(pen);
logger.info("Total Price "+totalPrice);
} catch (Exception e) {
logger.error("Exception "+e);
e.printStackTrace();
}
return totalPrice;
}
}
package com.junit.service;
public interface CalculatorService {
public double add(double input1, double input2);
public double subtract(double input1, double input2);
public double multiply(double input1, double input2);
public double divide(double input1, double input2);
}
\ No newline at end of file
package com.junit.service;
import java.util.List;
import com.junit.bean.Product;
public interface ProductService {
public List<Product> getProducts();
public Double getTotalPrice(Product pens);
}
package com.junit.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.junit.bean.Product;
@Service
public class ProductServiceImpl implements ProductService{
Product pen = new Product(101, "Pen", 15, 10);
Product book = new Product(102, "Book", 30, 10);
Product bag = new Product(101, "Bag", 200, 5);
List<Product> productList = new ArrayList<>();
@Override
public List<Product> getProducts() {
productList.add(pen);
productList.add(book);
productList.add(bag);
return null;
}
@Override
public Double getTotalPrice(Product pens) {
double toalPrice = pens.getPrice() * pens.getQuantity();
return toalPrice;
}
}
package com.junit.resource;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import com.junit.bean.Product;
import com.junit.service.ProductService;
import com.junit.service.ProductServiceImpl;
class ProductResourceTest {
/*
* @Mock ProductService productService;
*/
@Test
void testGetPrice() {
ProductService productService = mock(ProductService.class);
Product pen = new Product(101, "Pen", 15, 10);
when(productService.getTotalPrice(pen)).thenReturn(150.0);
assertEquals(productService.getTotalPrice(pen), 150.0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment