我使用JUnit4和Mockito创建了一个Spring Boot2应用程序。当我测试某种方法时。会出现这样的例外:
这是我的测试代码
@RunWith(MockitoJUnitRunner.class)
public class SurveyServiceTest {
@MockBean
SurveyRepository repostory;
@InjectMocks
SurveyService service;
@Test
public void getSurveyList() {
when(repostory.findAll()).thenReturn(Arrays.asList( new Survey(new Long(1),101,"Test1"),
new Survey(new Long(2),102,"Test2") ));
assertTrue(service.getSurveyList().size() >0);
}
}
SuveryService.java
@Service
public class SurveyService {
@Autowired
private SurveyRepository repostory;
public List<Survey> getSurveyList() {
return repostory.findAll();
}
public Optional<Survey> getSurveyById() {
return repostory.findById((long) 1);
}
public Survey add() {
Survey survey = new Survey();
survey.setSurveyID(1);
survey.setSurveyContent("ddddd");
return repostory.save(survey);
}
public Survey update() {
Survey survey = new Survey();
survey.setSurveyID(1);
survey.setSurveyContent("gggg1");
return repostory.save(survey);
}
public void delete() {
repostory.deleteById((long) 1);
}
public List<Survey> findBySurveyContent() {
return repostory.findBySurveyContent("gggg1");
}
public int updateBySurveyId(){
return repostory.updateBySurveyId("hhhhhh", 1);
}
}
SurveyRepository.java
public interface SurveyRepository extends JpaRepository<Survey, Long> {
public List<Survey> findBySurveyContent(String surveyContent);
@Query(value = "update XXX_DATA.SURVEYS set SURVEYCONTENT=? where SURVEYID=?",nativeQuery = true)
@Modifying
@Transactional
public int updateBySurveyId(String surveyContent,int surveyId);
}
@Entity
@Table(name="SURVEYS", schema="XXX_DATA")
public class Survey{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "SURVEYID")
private Integer surveyID;
@Column(name = "SURVEYCONTENT")
private String surveyContent;
public Survey(){
}
public Survey(Long id,Integer surveyID,String surveyContent){
this.id = id;
this.surveyID = surveyID;
this.surveyContent = surveyContent;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getSurveyID() {
return surveyID;
}
public void setSurveyID(Integer surveyID) {
this.surveyID = surveyID;
}
public String getSurveyContent() {
return surveyContent;
}
public void setSurveyContent(String surveyContent) {
this.surveyContent = surveyContent;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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.xxx.test</groupId>
<artifactId>xxx-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>xxx-service</name>
<description>XXX Service</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<repositories>
<repository>
<id>maven-repository</id>
<url>file:///${project.basedir}/maven-repository</url>
</repository>
<repository>
<id>com.ibm.db2.jcc</id>
<url>https://artifacts.alfresco.com/nexus/content/repositories/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>10.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.swat.password</groupId>
<artifactId>cwa2</artifactId>
<version>2.3.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
我引用了这篇文章Mockito NullPointerException,但这个问题仍然存在。任何帮助都将被应用。
@mockbean
是一个Spring注释,它只有在创建Spring Boot集成测试并使用SpringRunner
运行时才起作用。
这里有一个单元测试,使用mockitojunitrunner
运行,您只希望Mockito创建您的模拟存储库。正确的注释是@mock
。
我有这个过滤器类,在使用junit进行测试时需要尽可能高的代码覆盖率。 和测试等级: 当我运行时,它在 线 我如何避免这种情况? 我需要调用这个方法并执行里面的任何内容来提供所需的代码覆盖。
CalculatorOperationSmockTest.java 我成功地使用测试了上面的,但是我是Mockito框架的新手,当我尝试运行文件时,我得到了下面的错误,我不知道如何解决它
我正在为我的应用程序创建一个Spring BootAPI。我试图使用mockito对我的服务实现进行单元测试,以模拟出细节。该服务将向数据库添加一个新的构建实体。下面是服务实现和测试实现。楼宇服务: BuildingServiceImpl_UT
我正在尝试运行一个使用Mockito的JUnit cucumber测试。这是我遇到的问题。在我的cucumber赛跑课上,我有 在我的常规JUnit测试中 鉴于我一次只能有一个@RunWith,我如何将Mockito与cucumber结合使用呢?
问题内容: 这是从这个问题开始的:要求我在哪里开始一个新的问题。 问题是我只是对JUnit 或有关的东西之类的东西还不够了解,无法用Jeff Bowman提到的方式解决问题。 问题答案: 在您以后的评论中,我指出了差距:您需要使用Mockito作为规则,而将参数化为Runner,而不是相反。 原因是Runner负责报告测试数量,而Parameterized则根据测试方法的数量和参数化输入的数量来操
问题内容: 有人可以帮我这个忙。我正在使用Jersey休息测试框架版本2.21(在Grizzly容器上)编写Rest资源的单元测试。 当我调试测试类时,看到myManager的模拟对象。但是,当调试进入“ MyResouce类”时,myManager对象将变为null并得到NullPointer异常。 尝试过其他人提供的解决方案,但是没有运气。请有人帮我。我将近三天就遇到这个问题。:( 我的资源类