gpt4 book ai didi

spring - 每次我尝试运行我的 Junit 测试用例时都出现 NullPointer 异常

转载 作者:行者123 更新时间:2023-12-01 03:44:56 27 4
gpt4 key购买 nike

每次我尝试运行 junit 测试用例时,我都会得到空指针异常,并且每次函数返回 NULL 值时我都试图处理异常。这是代码。

RestaurantRepository.java

package org.springsource.restbucks;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


@Component
@Transactional
public interface RestaurantRepository extends PagingAndSortingRepository<Restaurant, Long> {
@Query("SELECT p FROM Restaurant p Where (p.id)=(:Id) and p.deleted=false")
Restaurant findById(@Param("Id") long Id);

@Query("SELECT p FROM Restaurant p Where LOWER(p.name)=LOWER(:name) and p.deleted = false")
Restaurant findByName(@Param("name") String name);
}

RestaurantController.java
@RestController
public class RestaurantController {
@Autowired
RestaurantRepository repository;
@RequestMapping(value = "/restaurants/{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ResponseEntity<Restaurant> getRestaurant(@PathVariable("id") long restaurantId) {
Restaurant responseRestaurant = repository.findOne(restaurantId);
if(responseRestaurant==null){
logger.error("No Restaurant found with id:"+restaurantId);
return new ResponseEntity<Restaurant>(HttpStatus.NOT_FOUND);
}else if(responseRestaurant.isDeleted()==true){
logger.error("Restaurant Object is deleted");
return new ResponseEntity<Restaurant>(HttpStatus.NOT_FOUND);
}else{
return new ResponseEntity<Restaurant>(responseRestaurant,HttpStatus.OK);
}
}

}

RestaurantRepositoryTest.java
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class RestaurantRepositoryTest{

@Autowired
private RestaurantRepository repository;

@Test
public void testfindById() throws Exception{ //testing findbyid method in restaurant repository

Restaurant responseRestaurant = repository.findByName("xyz"); //getting error over here
assertEquals("xyz",responseRestaurant.getName());
}
}

每当我运行 时,我都会收到空指针异常mvn 测试 .堆栈跟踪低于我在终端中显示的内容。
  • testfindById(org.springsource.restbucks.RestaurantRepositoryTest)
    已用时间:0.018 秒 <<< 错误! java.lang.NullPointerException:
    空在
  • org.springsource.restbucks.RestaurantRepositoryTest.testfindById(RestaurantRepositoryTest.java:19)

  • 我该怎么做才能成功运行我的测试?

    最佳答案

    您将它作为没有 Spring 上下文的纯单元测试运行。因此,存储库不会 Autowiring 。您应该能够通过在测试类的顶部添加一些注释来解决这个问题。

    如果使用 Spring Boot,那么类似以下内容应该可以工作,让 Spring Boot 完成所有艰苦的工作:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = MyApplication.class)

    您也可以加载 Spring @Configuration将初始化您的存储库的类(在我的情况下,这称为 DbConfig ),如下所示:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = { DbConfig.class },
    loader = AnnotationConfigContextLoader.class)

    关于spring - 每次我尝试运行我的 Junit 测试用例时都出现 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27816352/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com