@RunWith(PowerMockRunner.class)
@PrepareForTest(Topic_Service.class)
class Topic_Service_test
{
private Topic_Service topic_service = PowerMockito.spy(new Topic_Service());
@Test
void testing_test() throws Exception
{
PowerMockito.doReturn("hello").when(topic_service, "test_help");
String s = topic_service.testing();
Assertions.assertEquals("hello testing",s);
}
}
@Service
public class Topic_Service
{
public static String test_help()
{
return "test_help";
}
public String testing()
{
return test_help() + " " + "testing";
}
}
POM.XML Dependencies
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version> 2.8.47</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
错误:-
警告:发生了非法的反射访问操作警告:org.powermock.reflect.internal.WhiteboxImpl(文件:/home/an.kumar1/.m2/repository/org/powermock/powermock-reflect/1.7.0/powermock-reflect-1.7.0的非法反射访问。 jar)方法java.lang.Object.finalize()警告:请考虑将此报告给org.powermock.reflect.internal.WhiteboxImpl的维护者警告:使用--illegal-access = warn启用有关进一步非法反射访问操作的警告警告:所有非法访问操作将在以后的版本中被拒绝
org.opentest4j.AssertionFailedError:预期:您好测试实际的:null
有人可以帮忙吗?在此先感谢
在类批注@RunWith(PowerMockRunner。class)上使用它
@RunWith (PowerMockRunner.class)
class Topic_Service_test
{
private Topic_Service topic_service = PowerMockito.spy(new Topic_Service());
@Test
void testing_test() throws Exception
{
PowerMockito.doReturn("hello").when(topic_service, "test_help");
String s = topic_service.testing();
Assertions.assertEquals("hello testing",s);
}
}