Unit Testing with Mock and InvocationOnMock - java

I'm new with Java Spring, I need a guidance with Mockito.
I tried to test my Service Layer but it keeps failing to verify the mock username and email address.
I'm currently really confused on how should I do it properly.
Thanks you!
UserService.java
package com.example.newproject.newproject.service;
import com.example.newproject.newproject.entity.User;
import java.util.List;
public interface UserService {
boolean addUser(String username,String email);
List<User> viewAllUsers();
}
UserServiceImpl.java
package com.example.newproject.newproject.service.impl;
import com.example.newproject.newproject.entity.User;
import com.example.newproject.newproject.repository.UserRepository;
import com.example.newproject.newproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
#Service
#Transactional
public class UserServiceImpl implements UserService {
#Autowired
private UserRepository userRepository;
#Override
public boolean addUser(String username, String email) {
if(userRepository.findByUsernameAndEmail(username, email) == null)
{
User user = new User();
user.setUsername(username);
user.setEmail(email);
userRepository.save(user);
return false;
}else{
return true;
}
}
#Override
public List<User> viewAllUsers() {
return userRepository.findAll();
}
}
My Mock
#Test
public void testAddUser(){
Mockito.when(userRepository.findByUsernameAndEmail("Google","google#google.com")).then(invocationOnMock -> {
User user = new User();
return user;
});
boolean result = userService.addUser("Google","google#google.com");
Assert.assertFalse(result);
Mockito.verify(userRepository, Mockito.times(1)).save(userArgumentCaptor.capture());
User user = userArgumentCaptor.getValue();
Assert.assertEquals("Google", user.getUsername());
Assert.assertEquals("google#google.com", user.getEmail());
}
The error I get is
java.lang.AssertionError
during run

Related

How do I test my CustomUserDetailsService?

I have a CustomerUserDetailsService class, which is part of an implementation of Spring Boot authentication. I have tested my code manually, and it works correctly, allowing me to successfully log in to my app. However, I am unable to work out how to test the CustomerUserDetailsService.
As a unit test, I believe I would need to mock the userRepository member variable, but whatever I try in my test fails, the reason being userRepository is set to null. I've tried adding the #Repository annotation to userRepository but am told that is not allowed because it is an interface. There's also the fact that loadUserByUsername returns an instance of CustomUserDetails, which is just an implementation of an interface, so I have no idea how I'd mock that dependency?
I've also tried doing some kind of integration test but had similar issues, probably due to incorrect annotations on the test itself. In an ideal world, I'd prefer a unit test and would be extremely grateful for any guidance as to how to set it up.
These are the relevant classes...
CustomerUserDetailsService:
package com.phil.urlshortener.security;
import com.phil.urlshortener.model.User;
import com.phil.urlshortener.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
public class CustomUserDetailsService implements UserDetailsService {
#Autowired private UserRepository userRepository;
#Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return new CustomUserDetails(user);
}
}
User:
package com.phil.urlshortener.model;
import lombok.Data;
import javax.persistence.*;
#Data
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(nullable = false, unique = true, length = 50)
private String username;
#Column(nullable = false, length = 64)
private String password;
}
UserRepository:
package com.phil.urlshortener.repositories;
import com.phil.urlshortener.model.User;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {
#Query("SELECT u FROM User u WHERE u.username = ?1")
User findByUsername(String username);
}
CustomUserDetails:
package com.phil.urlshortener.security;
import com.phil.urlshortener.model.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
public class CustomUserDetails implements UserDetails {
private final User user;
public CustomUserDetails(User user) {
this.user = user;
}
#Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
#Override
public String getPassword() {
return user.getPassword();
}
#Override
public String getUsername() {
return user.getUsername();
}
#Override
public boolean isAccountNonExpired() {
return true;
}
#Override
public boolean isAccountNonLocked() {
return true;
}
#Override
public boolean isCredentialsNonExpired() {
return true;
}
#Override
public boolean isEnabled() {
return true;
}
}
Maybe you forgot to use mockito annotation #RunWith(MockitoJUnitRunner.class).
Here it's the unit test for CustomUserDetailsService :
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
#RunWith(MockitoJUnitRunner.class)
public class CustomUserDetailsService {
#Mock
private UserRepository userRepository;
private CustomUserDetailsService customUserDetailsService;
#Before
public void setUp() throws Exception {
customUserDetailsService = new CustomUserDetailsService(userRepository);
}
#Test
public void GIVEN_username_THEN_return_user_details() {
//Arrange
final String username = "existingUserName";
final User user = mock(User.class);
when(userRepository.findByUsername(username)).thenReturn(user);
//Act
final UserDetails userDetails = customUserDetailsService.loadUserByUsername(username);
//Assert
assertNotNull(userDetails);
assertEquals(user, ReflectionTestUtils.getField(userDetails, "user"));
}
}

WebSecurityConfiguration error in Java SpringBoot, not recognizing my user constructor

i'm building a Java SpringBoot(Back-End),Vuejs(front)aplication , and im on this part of seting the web security application.
On my User Class the constructor might be like this :
package com.miniAmazon;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO,generator = "native")
#GenericGenerator(name="native",strategy="native")
private Long id;
#OneToMany(mappedBy = "users",fetch= FetchType.EAGER)
Set<Product> productsSet= new HashSet<>();
#OneToMany(mappedBy="users", fetch=FetchType.EAGER)
Set<Purchase> purchaseSet=new HashSet<>();
private String userRole;
private String userName;
private String userEmail;
private String userPassword;
public User(){}
public User( String userEmail,String userName,String userPassword,String userRole){
this.userName=userName;
this.userEmail=userEmail;
this.userPassword=userPassword;
this.userRole=userRole;
}
public void addPurchase(Purchase purchase){ purchaseSet.add(purchase);}
public Set<Purchase>getUserPurchaseSet(){
return purchaseSet;
}
////////////////////////////////////////////setter/////////////////////////////////////////////
///////////////////////////////////////////getters////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
#Override
xxxxx
}
}
user repository like this:
package com.miniAmazon;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
#RepositoryRestResource
public interface UserRepository extends JpaRepository<User,String> {
User findByuserName (String usertName);
}
and the web security application code was settled in this way:
package com.miniAmazon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.GlobalAuthenticationConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#Bean
public PasswordEncoder passwordEncoder(){
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
#Bean
public CommandLineRunner initData(ProductRepository productRepository, UserRepository userRepository, PurchaseRepository purchaseRepository){
return(args)->{
User user1=new User("lolo#gmail.com","lolo gomex",passwordEncoder().encode("24"),"buyer");
User user2=new User("jhony#gmail.com","Jack Ripper",passwordEncoder().encode("mole"),"buyer");
User user3=new User("gothic#gmail.com","demo gago",passwordEncoder().encode("adu"),"seller");
User user4=new User("grau#gmail.com","grau gomex",passwordEncoder().encode("24"),"seller");
User user5=new User("goiy#gmail.com","divan Ripper",passwordEncoder().encode("mole"),"buyer");
User user6=new User("gatti#gmail.com","guti gago",passwordEncoder().encode("adu"),"admin");
userRepository.save(user1);
userRepository.save(user2);
userRepository.save(user3);
userRepository.save(user4);
userRepository.save(user5);
userRepository.save(user6);
};
}
}
#Configuration
#EnableWebSecurity
class WebSecurityConfiguration extends GlobalAuthenticationConfigurerAdapter {
#Autowired
UserRepository userRepository;
#Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(inputName-> {
User user =userRepository.findByuserName(inputName);
if (user != null) {
return new User(user.getUserName(), user.getUserPassword(),
AuthorityUtils.createAuthorityList("USER"));--------------------ERROR
} else {
throw new UsernameNotFoundException("Unknown user: " + inputName);
}
});
}
}
#Configuration
#EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
// http.cors();///de heroku tambien
http.authorizeRequests()
.antMatchers("/mini/all_products").permitAll()
.antMatchers("mini/all_products/user_dashboard/purchase/{id}").permitAll()
.antMatchers("/mini/all_product/registering").permitAll()
.antMatchers("/h2-console/**").permitAll()
.antMatchers("/rest/**").hasAuthority("ADMIN")
.antMatchers("/**").hasAuthority("USER")
.anyRequest().fullyAuthenticated();
/////Autorizaciones y permisos para los distintos niveles de seguridad que tendria el usuario segun su casificacion
http.formLogin()
.usernameParameter("name")
.passwordParameter("password")
.loginPage("/api/login");
//
http.logout().logoutUrl("/api/logout");
http.csrf().disable();
http.exceptionHandling().authenticationEntryPoint((req, res, exc) -> res.sendError(HttpServletResponse.SC_UNAUTHORIZED));
http.formLogin().successHandler((req, res, auth) -> clearAuthenticationAttributes(req));
http.formLogin().failureHandler((req, res, exc) -> res.sendError(HttpServletResponse.SC_UNAUTHORIZED));
http.logout().logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());
// http.headers().frameOptions().disable();
http.headers().frameOptions().sameOrigin();
}
private void clearAuthenticationAttributes(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (session != null) {
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
}
}
}
but keeps throwing me an error in the first WebSecurityConfiguration in the cosntructor of the new User, saying once i hover it
Cannot resolve constructor 'User(java.lang.String, java.lang.String, java.util.List<org.springframework.security.core.GrantedAuthority>)'
Any idea about why this is happening .Really sorry for having exposed all this code!!.
And thanks in advance!!!
I have checked your code. In your WebSecurityConfig class
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(inputName-> {
User user =userRepository.findByuserName(inputName);
if (user != null) {
return new User(user.getUserName(), user.getUserPassword(),
AuthorityUtils.createAuthorityList("USER"));--------------------ERROR
}
});
}
You are returning user object like this :
return new User(user.getUserName(),
user.getUserPassword(),
AuthorityUtils.createAuthorityList("USER"));
which is wrong.
Because you have created constructor with different argument:
public User(String userEmail,
String userName,
String userPassword,
String userRole);
So pass arguments correctly. It will work.
You can also return like this:
return new User(null, user.getUserName(), user.getUserPassword(),
AuthorityUtils.createAuthorityList("USER"));
You should return org.springframework.security.core.userdetails.User instead of your com.miniAmazon.User:
if (user != null) {
return new org.springframework.security.core.userdetails.User(user.getUserName(), user.getUserPassword(),
AuthorityUtils.createAuthorityList("USER"));
} else {
throw new UsernameNotFoundException("Unknown user: " + inputName);
}
since the loadUserByUsername method from UserDetailsService return a org.springframework.security.core.userdetails.UserDetails instance and org.springframework.security.core.userdetails.User implements org.springframework.security.core.userdetails.UserDetails.
public interface UserDetailsService {
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}

Verify UserName and Password in Spring MVC hibernate

I have this code of hibernateTemplate to verify the username and password but this code was working fine with MYSQL but failed in Oracle and was showing some problem in DAO class as it created the table but not saving the value.For project it was necessary to use Oracle
package com.infotech.dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.infotech.dao.StudentDAO;
import com.infotech.model.Student;
#Repository("studentDAO")
public class StudentDAOImpl implements StudentDAO {
#Autowired
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
#Override
public boolean saveStudent(Student student) {
int id = (Integer)hibernateTemplate.save(student);
if(id>0)
return true;
return false;
}
#SuppressWarnings("unchecked")
#Override
public Student getStudentDetailsByEmailAndPassword(String email,String password){
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Student.class);
detachedCriteria.add(Restrictions.eq("email", email));
detachedCriteria.add(Restrictions.eq("password", password));
List<Student> findByCriteria = (List<Student>) hibernateTemplate.findByCriteria(detachedCriteria);
if(findByCriteria !=null && findByCriteria.size()>0)
return findByCriteria.get(0);
else
return null;
}
}
**Now I again changed the DAO class instead of HibernateTemplate I used Session Interface and now value are saving in Database but the Validation of username and password is not taking place some issues in Hibernate Template.Please suggest changes instead of hibernate template what should I use
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.infotech.dao.StudentCredentialDao;
import com.infotech.model.Student;
import com.infotech.model.StudentCredential;
#Repository("StudentCredentialDAO")
public class StudentCredentialDaoImpl implements StudentCredentialDao {
#Autowired
private SessionFactory sessionFactory;
#Autowired
private HibernateTemplate hibernateTemplate;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Override
public boolean saveStudentCred(StudentCredential stuCred) {
Session session = this.sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(stuCred);
tx.commit();
session.close();
return true;
}
#SuppressWarnings("unchecked")
#Override//This method is creating problem as I need to use something else other than Hibernate Template
public StudentCredential getStudentDetailsByUnameAndPassword(String uname,String password){
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(StudentCredential.class);
detachedCriteria.add(Restrictions.eq("uname", uname));
detachedCriteria.add(Restrictions.eq("password", password));
List<StudentCredential> findByCriteria = (List<StudentCredential>)hibernateTemplate.findByCriteria(detachedCriteria);
if(findByCriteria !=null && Hibernate template .size()>0)
return findByCriteria.get(0);
else
return null;
}
}
#Repository
public class LoginDAOImpl implements LoginDAO {
#Autowired
private SessionFactory sessionFactory;
#Override
public String loginCheck(String customerID, String password) {
Session currentSession = sessionFactory.getCurrentSession();
Query theQuery = currentSession.createQuery("from UserAccount u where u.customerID=:id AND u.password=:pass");
theQuery.setParameter("id", customerID);
theQuery.setParameter("pass", password);
List results = theQuery.list();
if ((results!=null) && (results.size()>0)){
return "success";
}
else {
return "failed";
}
}
You could use in this way

Row count for login concept in springboot application

I am trying to create a login concept with spring boot application, so i just need row count of a mysql table, I tried with various internet answers but nothing got solution, please your help and suggestion is appriciate. I will mention my code bellow.
Controller
#GetMapping("/Login")
public String Login(Model model) {
model.addAttribute("customer", new Customer());
return "login";
}
#PostMapping("/LoginProcess")
public String LoginProcess(#ModelAttribute("customer") Customer thecustomer,HttpSession session) {
System.out.println(thecustomer);
Customer result = customerservice.Login_service(thecustomer.getUserName(),thecustomer.getPassword());
if(result==null)
{
return "login";
}
else if(result.getRole().equals("1"))
{
return "admindash";
}
else
{
//session.setAttribute("jsp_uname", result.getUserName());
return "customerdash";
}
}
CustomerImplDao
import javax.persistence.EntityManager;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.example.filedemo.model.Customer;
#Repository
public class CustomerDaoImpl implements Customerdao {
#Autowired
private EntityManager entityManager;
#Override
public void save(Customer theCustomer) {
Session cursession = entityManager.unwrap(Session.class);
//If Id=0 then It will do insert or id=x then it will do update
cursession.save(theCustomer);
}
#Override
public Customer Login(String username,String password) {
Session cursession = entityManager.unwrap(Session.class);
String hql="from Customer c where c.UserName=:username and c.password=:password";
Query<Customer> query = cursession.createQuery(hql,Customer.class);
query.setParameter("username", username);
query.setParameter("password", password);
Customer theCustomer = query.uniqueResult();
System.out.println("******************"+query.getResultList().size());
return theCustomer;
}
}
You can simply use:
query.getResultList().size();
Or on database siede:
entityManager.creteQuery("select count(c) from Customer c where c.UserName=:username and c.password=:password", Long.class).getSingleResult();
To do that, you need to inject it on top of your service (bean):
#Autowired
private EntityManager entityManager;

My controller is working in runtime, but the mockkmvc test is failing for a dependency that is not even in the class

When I run the test, I get a dependency error of UserService is not able to be found to be injected. This is weird because no where in ConstantsController.java did I use UserService. Also, UserService is label properly with #Service annotation.
I have tried using the #MockBean annotation in the controller test class. It gave me unidentifiable errors. I even tried autowiring the bean in the configuration because the log said define bean of type of UserService in configuration. Still no luck.
UserService
package com.GMorgan.RateMyFriendv5.Service;
import com.GMorgan.RateMyFriendv5.Entitiy.Role;
import com.GMorgan.RateMyFriendv5.Entitiy.User;
import com.GMorgan.RateMyFriendv5.Repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
#Slf4j
#Service
public class UserService {
private UserRepository repository;
public boolean login(String username, String password) {
List<User> userList = repository.findByUsername(username);
boolean isSuccessful = userList.get(0).isPassword(password);
log.info("Username: {} isSucessful: {}", username, isSuccessful);
return isSuccessful;
}
public boolean signUp(String email, String username, String password) {
if (userEmailExists(email) || userUsernameExists(username)) return false;
User user = new User();
user.setEmail(email);
user.setUsername(username);
user.setPassword(password);
user.setRole(Role.USER);
repository.save(user);
log.info("User email: {} username: {}", email, username);
return true;
}
public boolean userEmailExists(String email) {
return !repository.findByEmail(email).isEmpty();
}
public boolean userUsernameExists(String username) {
return !repository.findByUsername(username).isEmpty();
}
}
ConstantsController.java
package com.GMorgan.RateMyFriendv5.Controller;
import com.GMorgan.RateMyFriendv5.Utils.Mappings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class ConstantsController {
#Value("${controller.constant.ping.message}")
public String pingMessage;
#RequestMapping(Mappings.PING)
public String ping() {
return pingMessage;
}
}
ConstantsControllerTest
#RunWith(SpringRunner.class)
#WebMvcTest
#AutoConfigureMockMvc
public class ConstantsControllerTest {
#Autowired
private MockMvc mockMvc;
#Value("${controller.constant.ping.message}")
public String pingMessage;
#Test
public void pingTest() throws Exception {
this.mockMvc.perform(get(Mappings.PING)).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString(pingMessage)));
}
}
I want the test to pass. In runtime, when I go to the link, I see the ping message. I want the test to do so as well.
you need to specify your controller that you want to test:
Add #WebMvcTest(ConstantsController.class) to your annotation

Categories