-1

I'm working on a spring boot project and its controller, service, repositories are working fine. I tested them with POSTMAN.

I'm new to mockito and trying to test my project. I have following test class:

package com.spr;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

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.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import com.spr.entity.User;
import com.spr.repository.UserRepository;
import com.spr.service.UserService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {

@Autowired
private UserService userService;

@MockBean
private UserRepository userRepository;

@Before
public void init() {
}

@Test
public void getAllUsersTest() {
    List<User> users = new ArrayList<User>();

    users.add(new User("User1", "user1@email.com", "male"));
    users.add(new User("User2", "user2@email.com", "female"));

    when(userRepository.findAll()).thenReturn(users);

    List<User> userList = userService.getUsers();

    assertEquals(2, userList.size());
    verify(userRepository, times(1)).findAll();
}
}

I'm using Spring Source ToolSuite 3.9.9

I right clicked on project -> Run as -> jUnit Test

I get this error:

Caused by: org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface com.spr.repository.UserRepository.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.


Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.5-b02
JVM name           : Java HotSpot(TM) 64-Bit Server VM
JVM version        : 1.8.0_05-b13
JVM info           : mixed mode
OS name            : Windows 8.1
OS version         : 6.3

What am I doing wrong here?

Edit:

Here is my pom.xml and UserRepository

<?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     https://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.9.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.spr</groupId>
<artifactId>testboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SprBoot</name>
<description>Demo project for Spring Boot</description>

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

<dependencies>
    <!-- spring mvc, rest -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Spring data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- unit test rest -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- mysql dependency -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- test patch operation need this -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- hot swapping, disable cache for template, enable live reload -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

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

</project>

The UserRepository

package com.spr.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.spr.entity.User;

public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User>{

}

I have mockito-core-2.23.4.jar in classpath

Ashutosh
  • 4,371
  • 10
  • 59
  • 105

1 Answers1

0

The problem was Java version. I was using java version "1.8.0_05", I upgraded it to java version "1.8.0_221" and it worked.

Ashutosh
  • 4,371
  • 10
  • 59
  • 105