代码:

package com.atguigu.gulimall.thirdparty;import com.aliyun.oss.OSSClient;
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.test.context.junit4.SpringRunner;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;@SpringBootTest
public class GulimallThirdPartyApplicationTests {@AutowiredOSSClient ossClient;@Testpublic void testUpload() throws FileNotFoundException {InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Pictures\\removeAll.png");ossClient.putObject("gulimall-kaisarh", "removeAll.png", inputStream);ossClient.shutdown();System.out.println("上传完成");}@Testpublic void contextLoads() {}
}

错误
java.lang.NullPointerException

java.lang.NullPointerExceptionat com.atguigu.gulimall.thirdparty.GulimallThirdPartyApplicationTests.testUpload(GulimallThirdPartyApplicationTests.java:23)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)at org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

原因:
没有使用@RunWith(SpringRunner.class)注解

Springboot的@RunWith(SpringRunner.class)注解的意义在于:Test测试类要使用注入的类,比如@Autowired注入的类,

有了@RunWith(SpringRunner.class)这些类才能实例化到spring容器中,自动注入才能生效

不然直接一个NullPointerExecption

SpringBoot:java.lang.NullPointerException相关推荐

  1. SpringBoot中运行测试:java.lang.NullPointerException

    问题展示: SpringBoot中运行测试类报:java.lang.NullPointerException 问题描述: 提示:这里描述项目中遇到的问题: 在SpingBoot中当我们在它原有的测试基 ...

  2. 红米2支持java吗_红米2a会有报错:java.lang.NullPointerException

    离线打包版本用的是2016年3月16日发布的. 其他手机正常,目前只有红米2a会有报错:java.lang.NullPointerException log信息如下: java.lang.Runtim ...

  3. 报错:java.lang.NullPointerException 空指针异常

    项目场景 当用Java做项目管理系统登录页面的时,涉及到一些安全问题需要用到Shiro(Java安全锁框架).ajax, 问题描述: 当用到ajax做验证登录的时候,报空指针异常java.lang.N ...

  4. FAQ(76):java.lang.NullPointerException at com.xs.fun.base.proto.service.WorldChannelProtoBuffer

    2018年8月31日 1.日志: java.lang.NullPointerExceptionat com.xs.fun.base.proto.service.WorldChannelProtoBuf ...

  5. 绝望,上传文件失败。。遇到并解决java.lang.NullPointerException

    解决报错:java.lang.NullPointerException 解决方法:将文件的编码方式改为utf-8即可! 截图: 修改成功后: 代码: import os import json imp ...

  6. 【已解决】java.lang.NullPointerException at line 15, Solution.r

    报错: java.lang.NullPointerException at line 15, Solution.reversePrint at line 57, __DriverSolution__. ...

  7. java xml出错,Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException...

    Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException解决办法: 利用Java操作X ...

  8. java 捕获 nullpointerexception,Android上无法捕获java.lang.NullPointerException

    错误: java.lang.NullPointerException at com.lala.Home.hideLoadinganimation(Home.java:969) at com.lala. ...

  9. Java的Map使用get方法,报错 java.lang.NullPointerException

    下面代码运行会报错:java.lang.NullPointerException public class JZ39 {public int majorityElement(int[] nums) { ...

  10. 对象空指针原因之一---java.lang.NullPointerException: Attempt to invoke virtual method ‘void com.example.my

    代码复制下来格式不能自动调整,大家有什么好的办法可以call我,有点难受一行一行调. //开启一个线程来发送消息 new Thread(new Runnable() {@Overridepublic ...

最新文章

  1. 爬虫之使用verify参数忽略CA证书
  2. SAP MM 自定义条件类型出现在采购信息记录的'条件'界面里 ?
  3. linux 内核 proc_create 函数 内幕初探
  4. 解决 IPS forbidden 的问题
  5. 使用 Linux 系统调用的内核命令
  6. 网站移动版本开发踩坑实录二
  7. cmd52命令发送 mmc_乾坤合一~Linux SD/MMC/SDIO驱动分析(上)
  8. 使用python开发json、csv数据格式转换工具
  9. android安卓 通知notification
  10. 求n个排序链表的交集
  11. 视频教程-VBA从入门到精通(控件+类模块+ADO+SQL数据库)-Office/WPS
  12. 分享一个百度云加速下载工具
  13. python tokenize怎么用_tokenize -- 对 Python 代码使用的标记解析器 — Python 3.7.9 文档...
  14. 2020-12-20|西瓜更新Cookies ttwid获取方法
  15. 特斯拉降价也无法阻挡国内新能源汽车厂商前进的步伐
  16. 期货什么是涨跌(期货是看涨还是看跌)
  17. 找玩具 概率 dfs
  18. Selenium + Python 自学笔记(第九天)
  19. scp量子计算机,室温固态体系中实验实现绝热量子质因数分解
  20. AtCoder Beginner Contest 175 E.Picking Goods

热门文章

  1. 16.企业应用架构模式 --- 离线并发模式
  2. 14.卷2(进程间通信)---System V 共享内存区
  3. 42. HTTP Cookie
  4. 23. Element lastChild 属性
  5. hbase 二进制数据写入_HBase总结
  6. php中的上传全局变量 把全局变量的数组形式变得更简易
  7. struts标签的使用
  8. 虚拟参考站(VRS)
  9. 【Luogu】P1972HH的项链(链表+树状数组)
  10. MySQL将一张表的某些列数据,复制到另外一张表,并且修改某些内容