SpringBoot Junit单元测试

4 篇文章 0 订阅
4 篇文章 0 订阅

Junit单元测试

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.alibaba.dubbo.config.annotation.Reference;
import com.foo.pay.Application;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)//springboot 启动类
@ContextConfiguration(locations = { "classpath*:dubbo-consumer.xml" })
@WebAppConfiguration
public class BootJunitTest {
	
	@Reference(interfaceClass=ServiceA.class)
	private ServiceA serviceA;
	
	@Test
	public void testService(){
		System.err.println("SERVICE====="+serviceA);
	}
}

dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
	default-lazy-init="false">
		
	<dubbo:application name="dubbo-consumer-test" logger="slf4j" version="1.0"></dubbo:application>
	<dubbo:consumer retries="0" actives="100" timeout="10000" loadbalance="roundrobin"/>
	<dubbo:annotation/>
	<dubbo:registry address="zookeeper://**.**.**.**:2181" check="false" register="false" subscribe=""></dubbo:registry>

</beans>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为在Spring Boot中进行JUnit5的单元测试,你需要在测试类上使用@SpringBootTest注解。这个注解会加载整个Spring应用程序上下文,以便你可以使用Spring的功能和特性。一个简单的示例是这样的: ```java @SpringBootTest class MyUnitTest { // 测试方法 @Test void myTest() { // 测试逻辑 } } ``` 在测试方法中,你可以使用@Autowired注解来注入依赖的bean,并使用@Transactional注解标记方法以支持事务管理。@Transactional注解可以确保在测试完成后自动回滚,以保持测试的独立性。 与JUnit4相比,JUnit5是Spring Boot 2.2.0版本及以上的默认单元测试库。因此,如果你使用的是最新版本的Spring Boot,你应该使用JUnit5来编写你的单元测试。 在系统环境方面,你需要确保你的Java版本是8及以上,Maven版本是3.3及以上,以及使用IDEA作为开发工具。当你创建一个Maven项目时,你需要导入spring-boot-starter-test依赖来使用单元测试的场景。如果你使用的是Spring Initializr创建的项目,它将自动引入单元测试依赖,你不需要手动添加。 这是使用JUnit5进行Spring Boot单元测试的一般步骤。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [JUnit5单元测试(基于SpringBoot)](https://blog.csdn.net/maogenb/article/details/124459093)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值