the spring boot REST app with MySQL
I am trying to add record to DB and expect the record added and postman got it as output instead of the output "status":404,"error":"Not Found","path":"/api/employees" following is the classes
package com.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.service.EmployeeService;
import com.model.Employee;
#RestController
#RequestMapping("/api")
public class EmployeeController {
private EmployeeService employeeservice;
public EmployeeController(EmployeeService employeeservice) {
super();
this.employeeservice = employeeservice;
}
//build create Employee REST API
#PostMapping("/employees")
public ResponseEntity<Employee> saveEmployee(#RequestBody Employee employee)
{
return new ResponseEntity<Employee>(employeeservice.saveEmployee(employee), HttpStatus.CREATED);
}
}
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#EnableJpaRepositories(
basePackages = {"com.model"})
#SpringBootApplication(scanBasePackages = {"service.impl"})
public class RestapimysqlApplication {
public static void main(String[] args) {
SpringApplication.run(RestapimysqlApplication.class, args);
}
}
It doesn't look like you're scanning the package that contains your REST controller (i.e. com.controller), seems like you're only scanning the service.impl package on your RestapimysqlApplication class.
You should probably add the com.controller to the list of packages being scanned by Spring here:
#EnableJpaRepositories(basePackages = {"com.repository"})
#SpringBootApplication(scanBasePackages = {"service.impl", "com"})
#EntityScan({"com.model"})
public class RestapimysqlApplication {
...
}
Related
Here is the method for which I need to write the Junit test case.
Problem is here to write JUnit test cases for the following method which return the future
public Future<SomeResponse> getSomeResponse(SomeQuery someQuery){
if(dbCircuitBreaker == null){
dbCircuitBreaker= CircuitBreakers.getDbCircuitBreaker();
}
return dbCircuitBreaker.execute(future -> {
List<SomeResponseMetaData> SomeResponseMetaDataList = new ArrayList<>();
SomeResponse someResponse = new SomeResponse();
Observable<Some> someMetaDataObservable = someSQLRepository.findSomeDetails(someQuery);
someMetaDataObservable.subscribe(some -> {
getSomeResponse(someResponseMetaDataList,some);
},throwable -> {
FIDAL_LOGGER.error(CLASS_FULL_NAME, "getSomeResponse", LogConstants.UNCAUGHT_ERROR, "Could not able to fetch the data from some", throwable);
},()->{
FIDAL_LOGGER.info(CLASS_FULL_NAME, "getSomeResponse", LogConstants.UNCAUGHT_ERROR, "fetch the data for some is completed");
someResponse.setSomeLists(someResponseMetaDataList);
future.complete(someResponse);
});
});
}
This the not the best solution but I have written which is working fine for me.
import io.vertx.rxjava.circuitbreaker.CircuitBreaker;
import io.vertx.rxjava.core.Future;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import rx.Single;
import rx.observers.TestSubscriber;
import java.util.ArrayList;
import java.util.List;
#RunWith(PowerMockRunner.class)
#PowerMockIgnore("javax.management.*")
public class FileServiceHelperTest {
#Mock
SomeServiceHelper someServiceHelper = null;
#InjectMocks
CircuitBreaker dbCircuitBreaker = null;
#InjectMocks
SomeLineSQLRepository someLineSQLRepository = null;
#Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
#Test
public void getSomeResponse() {
SomeResponseMetaData someResponseMetaData = new SomeResponseMetaData();
someResponseMetaData.setRecievedDate("2020-02-12T03:57:01-0600");
someResponseMetaData.setFileName("29579_3_Order_File1_20200207050043852");
someResponseMetaData.setSomeId("43b61ab4-52b8-48e6-9489-b5824d278254");
someResponseMetaData.setSomeStatus("PROCESSED");
someResponseMetaData.setSubmitted("3");
someResponseMetaData.setProcessed("0");
someResponseMetaData.setPending("0");
someResponseMetaData.setErrors("3");
someResponseMetaData.setSomeType("ORDER");
someResponseMetaData.setShipNodeId("29579_3");
List<SomeResponseMetaData> someResponseMetaDataList = new ArrayList();
someResponseMetaDataList.add(someResponseMetaData);
SomeResponse someResponseExpected = new SomeResponse();
someResponseExpected.setSomeLists(someResponseMetaDataList);
Future<SomeResponse> someResponseFuture = Future.succeededFuture(someResponseExpected);
SomeQuery someQuery = new SomeQuery.SomeQueryBuilder("29579").setPageNo(0).setPageSize(1).build();
Mockito.when(someServiceHelper.getSomeResponse(someQuery)).thenReturn(someResponseFuture);
Single<SomeResponse> someResponseSingle = someServiceHelper.getSomeResponse(someQuery).rxSetHandler();
TestSubscriber<SomeResponse> testSubscriber = new TestSubscriber();
someResponseSingle.subscribe(testSubscriber);
testSubscriber.assertCompleted();
testSubscriber.assertNoErrors();
testSubscriber.assertValue(someResponseExpected);
}
}
This is the new file I've added in addition to the existing Persistenceconfig.Java. I'm getting this entity manager as 2 found.
we're not using any xml config except for the jpa repositories in spring-data.xml
The issues is occuring only for one package created newly for logging activity and that is also included in jpa repository.
Before adding the below class, everything is normal previously
package com.jumbotree.kumcha.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
//import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#Configuration
#EnableTransactionManagement
//#PropertySource("classpath:kumcha.properties")
#PropertySource("file:/opt/jumbotree/kumcha2/kms.properties")
#EnableJpaRepositories(basePackages = "com.jumbotree.kumcha.crm.model", entityManagerFactoryRef = "createEntityManagerFactoryChargingBean", transactionManagerRef = "createChargingTransactionManagerBean")
#ImportResource("classpath:spring-data.xml")
public class ChargingPersistenceConfig {
#Autowired
private Environment env;
private static final Logger LOGGER = LoggerFactory.getLogger(ChargingPersistenceConfig.class);
#Bean
public DataSource createChargingDataSourceBean() {
LOGGER.info("Charging Datasource created...");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("charging.db.driver"));
dataSource.setUrl(env.getProperty("charging.db.url"));
dataSource.setUsername(env.getProperty("charging.db.username"));
dataSource.setPassword(env.getProperty("charging.db.password"));
return dataSource;
}
//Here is the entity manager added and causing this issue
#Bean //(name = "entityManagerFactoryCharging")
#PersistenceContext (unitName="chargingPU")
public FactoryBean<EntityManagerFactory> createChargingEntityManagerFactoryBean(#Qualifier("createChargingDataSourceBean") DataSource dsc) {
LocalContainerEntityManagerFactoryBean chargingentityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
LOGGER.info("Charging entityman created...");
try {
chargingentityManagerFactoryBean.setDataSource(createChargingDataSourceBean());
chargingentityManagerFactoryBean.setPackagesToScan("com.jumbotree.kumcha.crm.model");
chargingentityManagerFactoryBean.setJpaVendorAdapter(createJpaVendorAdapterBean());
chargingentityManagerFactoryBean.setJpaProperties(createJpaProperties());
} catch (Exception e) {
// TODO: handle exception
LOGGER.error(e.toString());
}
return chargingentityManagerFactoryBean;
}
private Properties createJpaProperties() {
LOGGER.info("hibernate.show_sql :::: "+env.getProperty("hibernate.show_sql"));
return new Properties() {
{
// setProperty("hibernate.hbm2ddl.auto", "create-drop");
setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
// setProperty("hibernate.cache.use_second_level_cache", "true");
// setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
// setProperty("shared-cache-mode", "DISABLE_SELECTIVE");
//<property name="hibernate.cache.use_second_level_cache" value="true"/>
//setProperty("hibernate.connection.zeroDateTimeBehavior", "convertToNull");
}
};
}
private JpaVendorAdapter createJpaVendorAdapterBean() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
// jpaVendorAdapter.setDatabase(Database.valueOf(env.getProperty("db.name")));
jpaVendorAdapter.setShowSql(true);
// jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setDatabasePlatform(env.getProperty("hibernate.dialect"));
return jpaVendorAdapter;
}
#Bean //(name = "transactionManager")
public PlatformTransactionManager createChargingTransactionManagerBean() throws Exception {
LOGGER.info("Charging transactionMan created...");
JpaTransactionManager chargingtransactionManager = new JpaTransactionManager();
chargingtransactionManager.setEntityManagerFactory(createChargingEntityManagerFactoryBean(createChargingDataSourceBean()).getObject());
return chargingtransactionManager;
}
#Bean
public PersistenceExceptionTranslationPostProcessor createPersistenceExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
}
// Required if using Hibernate 4
#Bean
public PersistenceExceptionTranslator createPersistenceExceptionTranslatorBeaan() {
return new HibernateExceptionTranslator();
}
}
LoggerRepository cannot choose one of the 2 beans createChargingEntityManagerFactoryBean and createEntityManagerFactoryBean
Make one of them primary and/or specify qualifier. (BTW sometimes even with qualifier it's necessary to make one of beans primary)
Cannot suggest cconfig changes without your code.
I am getting a result from my unit test that I don't quite understand.
Controller Code
package com.rk.capstone.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.rk.capstone.model.domain.User;
import com.rk.capstone.model.services.user.IUserService;
/**
* REST Controller for /register endpoint
*/
#RestController
#RequestMapping("/register")
public class RegisterController {
private final IUserService userService;
public RegisterController(IUserService userService) {
this.userService = userService;
}
#RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity<User> registerNewUser(#RequestBody User user) {
if (userService.findByUserName(user.getUserName()) == null) {
user = userService.saveUser(user);
return ResponseEntity.status(HttpStatus.CREATED).body(user);
} else {
return ResponseEntity.status(HttpStatus.CONFLICT).body(null);
}
}
}
Unit Test Code:
package com.rk.capstone.controllers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rk.capstone.model.dao.UserDao;
import com.rk.capstone.model.domain.User;
import com.rk.capstone.model.services.user.IUserService;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Class Provides Unit Testing for RegisterController
*/
#RunWith(SpringRunner.class)
#WebMvcTest(RegisterController.class)
public class RegisterControllerTest {
#MockBean
private IUserService userService;
#Autowired
private MockMvc mockMvc;
private User user;
private String userJson;
#Before
public void setup() {
user = new User("rick", "k", "rick#email.com", "rkow", "abc123");
ObjectMapper objectMapper = new ObjectMapper();
try {
userJson = objectMapper.writeValueAsString(user);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
#Test
public void testRegisterNewUserPostResponse() throws Exception {
given(this.userService.findByUserName(user.getUserName())).willReturn(null);
given(this.userService.saveUser(user)).willReturn(user);
Assert.assertNotNull("Mocked UserService is Null", this.userService);
this.mockMvc.perform(post("/register/user").content(userJson).
contentType(MediaType.APPLICATION_JSON)).
andExpect(status().isCreated()).
andDo(print()).andReturn();
}
}
The result of the print() is below, I do not understand why the Body is empty. I have tried numerous things I've read on other posts and blogs and no matter what I try the Body is always empty. Adding a Content-Type header in the controller response makes no difference.
MockHttpServletResponse:
Status = 201
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
What is confounding me even more, is when I run the actual application and perform a POST using PostMan to the /register/user endpoint the response contains the body and status code I expect, a User represented via JSON, e.g.
Status Code: 201 Created
Response Body
{
"userId": 1,
"firstName": "rick",
"lastName": "k",
"emailAddress": "rick#email.com",
"userName": "rk",
"password": "abc123"
}
Any help or ideas is appreciated, using SpringBoot 1.4.0.RELEASE.
UPDATE: For some reason the following mocked method call is returning null in the controller under test.
given(this.userService.saveUser(user)).willReturn(user);
This thread ultimately turned me on to a solution:
Mockito when/then not returning expected value
Changed this line:
given(this.userService.saveUser(user)).willReturn(user);
to
given(this.userService.saveUser(any(User.class))).willReturn(user);
I am using power mockito and I am mocking a class SomeUtil having all static methods.
import java.util.List;
class SomeUtil {
// other static methods
public static X createX(String name, List<String> addresses, boolean isEnabled) {
// implementation
return null;
}
// other static methods
}
And I have mocked it as follows.
PowerMockito.mockStatic(SomeUtil.class, Answers.CALLS_REAL_METHODS.get());
Answer<Row> createXAnswer = new Answer<Row>() {
#Override
public Row answer(InvocationOnMock invocation) throws Throwable {
return new X();
}
};
PowerMockito.when(SomeUtil.createX(Mockito.any(String.class), Mockito.any(List.class), Mockito.any(Boolean.class)).thenAnswer(createXAnswer);
But with that createX() method always invokes original method, which I am trying to avoid. I suspect that because of boolean primitive type. Am I missing something here? Please guide
I think you have omitted some important annotations on your test class (#RunWithand #PrepareForTest). This is a complete example:
import static org.junit.Assert.assertSame;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(SomeUtil.class)
public class SomeUtilTest {
#Test
public void should_do_this() {
mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
final X x = new X();
when(SomeUtil.createX(anyString(), anyList(), anyBoolean())).thenReturn(x);
assertSame(x, SomeUtil.createX(null, null, true));
}
}
This example works but could in some case. For example if the createX raise an exception. fail sometime because the **real method createX is invoked ** * when we stub it:
when(SomeUtil.createX(anyString(), anyList(), anyBoolean())).thenAnswer(createXAnswer);
this is due to the default answer CALLS_REAL_METHODS:
mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
since the OP can't remove this default answer (see comment):
For only few methods I want to return our own Answer. For the rest of methods I want to call real methods.
I think you will have to use powermock api:
import static org.junit.Assert.assertSame;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.stub;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest(SomeUtil.class)
public class SomeUtilTest {
#Test
public void should_do_this() {
mockStatic(SomeUtil.class, Mockito.CALLS_REAL_METHODS);
final X x = new X();
stub(method(SomeUtil.class,
"createX",
String.class,
List.class,
boolean.class)).toReturn(x);
assertSame(x, SomeUtil.createX(null, null, true));
}
}
In my entities I have some hibernate annotations for validation, like #NotEmpty, #Pattern.. and others
In my controller, on save action, it has an #Valid parameter.
But if any entity has any required field, and there is no annotation I will have problems.
So I would like to test each entity, to ensure they have the necessary notes.
Something like:
#Test(expect=IllegalArgumentException.class)
public void testAllNull() {
Person p = new Persson(); // Person name has an #NotEmpty
validator.validate(p);
}
But how to validate it? Who is called to check #Valid?
Thanks.
I found out how to check:
#Autowired
private LocalValidatorFactoryBean validator;
...
validator.validateProperty(object, propertyName)
Here is a Spring v4.1.x based example of a test validating presence and processing of the #Valid annotation and building of custom JSON response in case of an error.
jUnit
import com.fasterxml.jackson.core.type.TypeReference;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import javax.inject.Inject;
import java.util.List;
import static org.abtechbit.miscboard.util.JsonUtils.toJson;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {
RegistrationValidationTest.MockDependencies.class,
})
public class RegistrationValidationTest {
#Inject
MockMvc mvc;
#Test
public void validatesRegistration() throws Exception {
Registration registration = ... //build an invalid Registration object
MvcResult result = mvc.perform(post(RegistrationController.CONTEXT_REGISTER).
contentType(MediaType.APPLICATION_JSON).
content(toJson(registration))).
andExpect(status().isBadRequest()).
andExpect(content().contentType(MediaType.APPLICATION_JSON)).
andReturn();
assertThat(result.getResolvedException(), is(notNullValue()));
String content = result.getResponse().getContentAsString();
assertThat(content, is(notNullValue()));
List<Message> messages = JsonUtils.fromJson(content, new TypeReference<List<Message>>() {
});
assertThat(messages.size(), is(1));
}
public static class MockDependencies {
#Bean
public MockMvc mvc() {
return MockMvcBuilders.standaloneSetup(new RegistrationController()).build();
}
}
}
Controller
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
#Controller
public class RegistrationController
{
public static final String CONTEXT_REGISTER = "/register";
#RequestMapping(value = CONTEXT_REGISTER, method = RequestMethod.POST)
#ResponseBody
public String register(#RequestBody #Valid Registration registration) {
//perform registration
}
#ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<List> handleValidationException(MethodArgumentNotValidException ex) {
//Build a list of custom Message{String message;} objects
List<Message> messages = ex.getBindingResult().getAllErrors().
stream().map(e->new Message(e.getDefaultMessage())).collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(messages);
}
}
Spring MVC Test Framework might be a good choice. By using this, you can be assured that validations in your tests runs codes as Spring #MVC actually works.
Actually, the #Valid annotation is detected by HandlerMethodInvoker, which processes annotations on the handler methods of Spring controllers. Internally, the actual validation logic is delegated to the Validator bean depending on your application context settings. (Hibernate Validator is widely used.)
By default configuration (e.g. <mvc:annotation-driven />), LocalValidatorFactoryBean is used internally to process #Valid annotation as #Falci noted, but it may differ time to time. Instead, Spring MVC Test Framework provides the same environment as the main application uses, hence a good choice.