简体   繁体   中英

Cannot find Java package powermock.api.mockito

I am trying to get Power Mockito to work in my java test. For the life of me I cannot get it to find it. I tried inserting the dependency in the pom file, rebuild project and run the tests, but it still will not find it. Any ideas why it will not find this?

Here's what I have in my MemberServiceTest.java file:

package com.cerner.revenuecycle.vbr.services;

import static org.junit.Assert.assertEquals;

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

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.hibernate.SessionFactory;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.powermock.api.mockito.PowerMockito; //***** cant find *****
import org.powermock.core.classloader.annotations.PrepareForTest; //**** cant find ****
import org.powermock.modules.junit4.PowerMockRunner; //***** cant find ****

@RunWith(MockitoJUnitRunner.class)
public class MemberServiceTest {

    @InjectMocks
    MemberService memberService;

    @Mock
    MemberHibernate memberHibernate;

    @Mock
    CaseHelper caseHelper;

    @Test
    public void testGetCaseNumber() {

        SessionFactory testSessionFactory = HibernateUtil.newTestSessionFactory();
        PowerMockito.mockStatic(HibernateUtil.class); //this doesnt work now


        PowerMockito.when(HibernateUtil.getSessionFactory()).thenReturn(testSessionFactory); //this doesnt work now 
        Case case1 = new Case();
        Case case2 = new Case();
        case1.setTenantId("1");
        case2.setTenantId("2");

        String caseNum1 = caseHelper.getNextCaseNumberFromDatabase(case1.getTenantId());
        String caseNum2 = caseHelper.getNextCaseNumberFromDatabase(case2.getTenantId());

        assertEquals(caseNum1, caseNum2);

    }
}

and in my pom.xml file, as you can see I've put the mockito and powermock dependencies in there:

<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>

    <groupId>com.cerner.revenuecycle.vbr</groupId>
    <artifactId>vbr-services-integration</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.cerner.revenuecycle.vbr</groupId>
        <artifactId>vbr-services-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <name>vbr-services-integration</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-jetty</artifactId>
            <version>2.17</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.17</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.skyscreamer</groupId>
            <artifactId>jsonassert</artifactId>
            <version>1.2.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.6</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.wagon</groupId>
                        <artifactId>wagon-ssh</artifactId>
                        <version>1.0-beta-7</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.maven.wagon</groupId>
                        <artifactId>wagon-webdav-jackrabbit</artifactId>
                        <version>1.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

Any idea why it is not finding the library for powermockito?

I don't know if you have tryed that but here go.

Try to force the update of the dependences "mvn clean install -U", and also try to check if the jar is in your m2. folder

Add this to your gradle:

    // required if you want to use Mockito for unit tests
    testCompile 'org.mockito:mockito-core:2.7.22'
    // required if you want to use Mockito for Android tests
    androidTestCompile 'org.mockito:mockito-android:2.7.22'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM