Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Url模拟测试上的java.lang.NullPointerException_Java_Junit_Nullpointerexception_Mocking_Powermock - Fatal编程技术网

Url模拟测试上的java.lang.NullPointerException

Url模拟测试上的java.lang.NullPointerException,java,junit,nullpointerexception,mocking,powermock,Java,Junit,Nullpointerexception,Mocking,Powermock,我的Url连接测试工作正常。一个月后,我运行了应用程序中的所有测试,URL连接测试失败,我不知道为什么,因为我没有更改这个类。拜托,有人能看看这个测试吗 测试现在抛出一个java.lang.NullPointerException 测试的最初想法来自: 测试: import org.junit.jupiter.api.Test; 导入org.junit.runner.RunWith; 导入org.mockito.mockito; 导入org.powermock.api.mockito.Power

我的Url连接测试工作正常。一个月后,我运行了应用程序中的所有测试,URL连接测试失败,我不知道为什么,因为我没有更改这个类。拜托,有人能看看这个测试吗

测试现在抛出一个
java.lang.NullPointerException

测试的最初想法来自:

测试:

import org.junit.jupiter.api.Test;
导入org.junit.runner.RunWith;
导入org.mockito.mockito;
导入org.powermock.api.mockito.PowerMockito;
导入org.powermock.core.classloader.annotations.PrepareForTest;
导入org.powermock.modules.junit4.PowerMockRunner;
导入java.io.IOException;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.net.URLConnection;
导入静态org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(PowerMockRunner.class)
@PrepareForTest({URL.class})
类JsonOutputFromURLImplTest
{
@试验
void currencyValuesNBP()引发IOException
{
类UrlWrapper
{
私有URL;
公共UrlWrapper(字符串规范)引发错误的URLdurException
{
url=新url(规范);
}
私有URLConnection openConnection()引发IOException
{
返回url.openConnection();
}
}
UrlWrapper url=PowerMockito.mock(UrlWrapper.class);
HttpURLConnection HttpURLConnection=Mockito.mock(HttpURLConnection.class);
PowerMockito.when(url.openConnection()).thenReturn(httpURLConnection);
assertTrue(HttpURLConnection的url.openConnection()实例);
}
}
测试等级:

@服务
@诺尔格构装师
公共类JsonOutputFromURLImpl实现JsonOutputFromURL
{
@凌驾
公共字符串currencyValuesNBP(字符串APIUrl)抛出带有APIEX的连接
{
字符串jsonOutput=null;
试一试{
URL=新URL(APIRL);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“GET”);
setRequestProperty(“内容类型”、“应用程序/json”);
InputStreamReader InputStreamReader=新的InputStreamReader(httpURLConnection.getInputStream());
BufferedReader BufferedReader=新的BufferedReader(inputStreamReader);
jsonOutput=bufferedReader.readLine();
httpURLConnection.disconnect();
}捕获(IOE异常){
e、 printStackTrace();
}
if(jsonOutput!=null)
返回jsonOutput;
使用apiex(apirl)抛出新连接;
}
}
java.lang.NullPointerException
在com.bartosz.kolej.stock.service.impl.JsonOutputFromURLImplTest$1UrlWrapper.openConnection(JsonOutputFromURLImplTest.java:36)
在com.bartosz.kolej.stock.service.impl.JsonOutputFromURLImplTest$1UrlWrapper.access$000(JsonOutputFromURLImplTest.java:25)
在com.bartosz.kolej.stock.service.impl.JsonOutputFromURLImplTest.currencyValuesNBP(JsonOutputFromURLImplTest.java:42)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:532)
位于org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:170)
位于org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:166)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:113)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:58)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$4(NodeTestTask.java:108)
位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:72)
在org.junit.platform.engine.support.hierarchy.NodeTestTask.executecurively上(NodeTestTask.java:98)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.execute(NodeTestTask.java:74)
forEach(ArrayList.java:1257)
位于org.junit.platform.engine.support.hierarchy.samethreadherarchycalTestExecutorService.invokeAll(samethreadherarchycalTestExecutorService.java:38)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$executecurively$4(NodeTestTask.java:112)
位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:72)
在org.junit.platform.engine.support.hierarchy.NodeTestTask.executecurively上(NodeTestTask.java:98)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.execute(NodeTestTask.java:74)
forEach(ArrayList.java:1257)
位于org.junit.platform.engine.support.hierarchy.samethreadherarchycalTestExecutorService.invokeAll(samethreadherarchycalTestExecutorService.java:38)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$executecurively$4(NodeTestTask.java:112)
位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:72)
在org.junit.platform.engine.support.hierarchy.NodeTestTask.executecurively上(NodeTestTask.java:98)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.execute(NodeTestTask.java:74)
位于org.junit.platform.engine.support.hierarchy.Sam
public static class MyClass {
    public String A() {
        System.out.println("A called");
        return "A";
    }

    private String B() {
        System.out.println("B called");
        return "B";
    }

}
@org.junit.Test
public void testMockPrivateMethod() throws Exception {
    MyClass myClass = PowerMockito.mock(MyClass.class);
    PowerMockito.when(myClass.B()).thenReturn("mockB");
    //PowerMockito.when(myClass, "B").thenReturn("mockB");
    System.out.println(myClass.B());
}

Output:
mockB
@org.junit.jupiter.api.Test
public void testMockPrivateMethod() throws Exception {
    MyClass myClass = PowerMockito.mock(MyClass.class);
    PowerMockito.when(myClass.B()).thenReturn("mockB");
    System.out.println(myClass.B());
}

Output:
B called

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.