gpt4 book ai didi

java - Aop调用异常 : in @Autowieired in JUnit

转载 作者:行者123 更新时间:2023-12-02 02:49:08 25 4
gpt4 key购买 nike

在我的 java spring mvc 应用程序中,我使用以下代码:

存储库:

@Repository
public interface TransactionRepository extends JpaRepository<Transactions ,Long >{

@Query(value = "SELECT sum( value) FROM Transactions inner join Customer on Transactions.customer_id=Customer.id where merchant_id= ?1 and age_class= ?2 ", nativeQuery=true)
public double getOverAllValue(String merchantID,String ageGroup);


}

然后我制作了一个基于此@Repository的服务:

@Service
public class IncomeDataService {

@Autowired
TransactionRepository transactionRepo;

public ArrayList<Double> dataCollector(int merchantID){
ArrayList<Double> data= new ArrayList<Double>();
for(int i=0;i<10;i++)
data.add( transactionRepo.getOverAllValue(Integer.toString(merchantID),Integer.toString(i)));

return data;
}

然后我尝试使用junit测试编写的服务:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SEBApplication.class)
public class IncomeDataServiceTest {

@Autowired
IncomeDataService incomeDataService;

@Test
public void testDataCollector() {
System.out.println(incomeDataService.dataCollector(1));
}

}

但是,当我运行测试时,它会提示:

org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract double ee.seb.domain.repository.TransactionRepository.getOverAllValue(java.lang.String,java.lang.String)

最佳答案

将查询的返回类型从原始数据类型 double 更新为 Double 因为您的查询可以返回将被包装类接受的 null 值而不是原始的,所以你的最终查询将是这样的:

@Query(value = "SELECT sum( value)  FROM Transactions  inner  join Customer on  Transactions.customer_id=Customer.id   where merchant_id= ?1 and age_class= ?2 ", nativeQuery=true)
public Double getOverAllValue(String merchantID,String ageGroup);

关于java - Aop调用异常 : in @Autowieired in JUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44094283/

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