我想让Power Mockito在我的java测试中工作。对于我的生活,我无法找到它。我尝试在pom文件中插入依赖项,重建项目并运行测试,但它仍然无法找到它。有什么想法,为什么它不会找到这个?
这是我在MemberServiceTest.java
文件中的内容:
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);
}
}
在我的pom.xml
文件中,您可以看到我已将mockito和powermock依赖项放在那里:
<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>
知道为什么找不到powermockito的库吗?
答案 0 :(得分:1)
我不知道你是否已经尝试过,但这里就去了。
尝试强制更新依赖项“mvn clean install -U”,并尝试检查jar是否在m2中。夹
答案 1 :(得分:0)
将此添加到您的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'