spring测试的2种方式

环境:spring 3.0.5 , junit 4.8.1

1 使用junit

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/applicationContext.xml","classpath:/applicationContext*.xml","classpath:/com/test/spring/hibernate.xml"})
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
@Transactional
public class BaseDAOTestCaseJunit {

@Autowired
YourDAO yourDAO; //这个换成要测试的DAO

@Autowired
YourDAO2 yourDAO2; //第2个DAO

@Before
public void init(){
System.out.println("init...");
}
//
@Test
public void getList(){ //junit的test方法
List l = yourDAO.yourMethod();
for(int i = 0 ; i< l.size(); i++){
System.out.println(l.get(i));
}
}

}



2 直接在main函数中调用spring

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;


/**
* SpringTest
* 就是把这个类注册成service,然后获取该类,调用方法。所以建个普通类,直接掉其他service也可以
* @author MaXin
*
*/
@Service("testSpring")
@Transactional
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)
public class TestSpring {

@Autowired
YourDAO YourDAO; //这个换成要测试的DAO

@Autowired
YourDAO2 yourDAO2; //第2个DAO

//
public void getList(){
List l = YourDAO.yourMethod(); //调用具体DAO的方法
for(int i = 0 ; i< l.size(); i++){
System.out.println(l.get(i));
}
}

public static void main(String [] args){
String[] configLocations = { "classpath*:/com/test/spring/hibernate.xml",
"classpath*:applicationContext.xml","classpath*:applicationContext*.xml" };
ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocations);
TestSpring one = (TestSpring) ctx.getBean("testSpring");
one.getList();
}



}



要是想点一下,把所有的东西都测试了,那就用junit吧。
不过要是遇到,不方便开web程序的,并且刚写完DAO,想试试性能的,就用第2种吧,很方便。
关于spring注解:[url]http://www.iteye.com/topic/295348[/url]
这里就强调一下:

<!--使用以下标签,让spring知道在哪找注解
xmlns:context="http://www.springframework.org/schema/context"
-->
<context:component-scan base-package="com.test"/>


Cron expressions:[url]http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger[/url]
基于Spring Boot2 + Spring Security OAuth2 实现单点登陆(一) 关于OAuth2的基本介绍:点这里 Spring Security 5.0开始对密码相关内容做了修改,具体查看这里 实现一个最基本的OAuth2认证 项目使用3个独立的工程分别实现认证服务、资源服务器和单点登陆服务器 源码地址 添加项目依赖 allprojects { apply plugin: 'idea' apply plugin: 'java' }... 阅读详情

相关推荐

Spring基础(十七):Spring日志框架和测试支持

在resources目录下准备log4j2.xml的配置文件。导入log4j2依赖。

Lansonli(蓝深李)的博客 1299

学习之谈

本文出处: http://bbs.chinaz.com/Archiver/Design/thread-487332-1.html开发工具: Microsoft Visual Studio .NET 2003  中文企业版 操作系统: Windows XP  英文专业版 题记 : 即使你没有一点编程经验,只要你愿意,并理解本文介绍的学习方法,你也可以象作者一样轻松、愉快地步入程序设计之门。让我们

yanyuhongchen的专栏 1079

EZscript 简单自动F5喝水程序

EZscript 第一步: 打开压缩包解压缩,运行注册文件 第二步: 运行程序EZscript,点选language,在跳出来的文件里选择ChineseGB.lng,确定,将程序换成中文语言 第三步: 点档案,点开启旧档,选择95%喝水.ezs 第四步: 登陆天堂游戏后,切换出游戏,点更新程序,在旁边的下拉框中选择乱码的天堂游戏进程,再点最右下角的检查,再点执行。就OK了。 ps:要喝的药水需放在F5位置

程序1

###有一个分数序列:2/1,3/2,5/3,8/5,13/8,21/13,………………求出该数的前二十项之和!!!!!   void main() {   int fz,fm;  int i, t;  float sum=0;  for(i=0;i  {   sum=sum+(fm/fz);   t=fz;   fz=fz+fm;   fm=t;  }   cout

hui_250的专栏 257

按键精灵网游脚本编写教程

按键精灵网游脚本编写教程按键精灵网游脚本编写教程按键精灵网游脚本编写教程按键精灵网游脚本编写教程

简单自动F5喝水程序.zip_limnem_lineage_喝水程序_天堂_天堂1

天堂1簡易版本喝水程式,可用於各種天堂版本 簡單使用,快樂遊戲

Spring的测试和调试方法

集成测试:测试多个组件之间的协作是否正确。总的来说,Spring提供了很多强大的测试和调试工具,可以帮助开发人员快速、高效地调试和测试应用程序,提高应用程序的质量和稳定性。调试日志:在Spring应用程序中,可以通过配置日志级别和使用日志框架来记录应用程序的运行情况,以便调试和分析问题。使用调试器:在IDE中使用调试器,可以逐步执行程序并查看变量的值和调用栈信息,以便更深入地调试和分析问题。使用断点:在IDE中使用断点,可以暂停程序的执行并查看变量的值和调用栈信息,以便调试和分析问题。

lonely_baby的博客 1056

立创EDA实战:基于NE555的夜间氛围呼吸灯电路设计与模式切换

本文详细介绍了使用立创EDA设计基于NE555芯片的夜间氛围呼吸灯电路。项目核心是利用NE555产生PWM信号,并通过RC充放电电路实现LED亮度的缓慢周期性变化,形成呼吸效果。文章手把手教学,涵盖从电路原理分析、元件参数计算,到在立创EDA中进行原理图绘制、PCB布局布线,直至焊接调试的完整硬件设计流程,是电子爱好者入门实战的经典项目。

weixin_42153793的博客 127

Struts2 不依赖Spring 的测试方式

Struts2 不依赖Spring 的测试方式 http://hi.baidu.com/%C9%AE_%CC%C6/blog/item/3560e91af41f92078618bf84.html2011-12-23 20:40package cn.wang.myh...

117

从零构建:ESP32S3无线DAP-Link调试器的硬件与软件协同设计

本文详细介绍了基于ESP32S3的无线DAP-Link调试器的硬件与软件协同设计,涵盖关键元器件选型、无线通信协议优化、双核任务处理及低功耗调试方案。通过ESP-NOW协议实现低延迟无线调试,结合USB协议栈与双核协同,为嵌入式开发提供高效灵活的调试解决方案。

gitlab7runner的博客 861

CESM笔记——CESM1.0-CESM1.2.2-CESM2.1.3三个版本模式工作流对比

最近测试了CESM1.0,CESM1.2.2和CESM2.1.3三个版本,不同版本在workflow上有一些区别,来做点笔记。 CESM1.0 测试compset:B_2000,F_2000 workflow: 1. create_newcase -case ~/CESM/cesm1_0/script/B_2000 -compset B_2000 -res f19_g16 -mach tianhe 2. cd B_2000 (optional) env_mach_pes.xml ##修改核数,

砍柴姑娘Jourosy的博客 4947

spring多个xml配置文件测试方式2

applicationContext.xml的配置文件 [color=red][/color]

jadewoo的专栏 235

两张 300I Duo 部署 Qwen3.5‑32B

开发者成功使用两张华为300IDuo(共4颗Ascend310P3芯片)部署Qwen3/Qwen3.5的30B/32B模型,并公开详细步骤。关键点包括:硬件需两张300IDuo(每芯片44GB显存),软件需CANN≥24.1和MindIE2.3.0专用镜像。部署流程涵盖驱动安装、模型下载(需修改为FP16格式)、容器启动、张量并行配置(TP=4)等环节。实测显示每芯片显存占用20-35GB,系统稳定运行。该方案已在知乎等平台验证可行,适用于Qwen3.5-32B等同类模型。

evering的博客 2259

Spring源码阅读2 -- Spring5.1.6源码ClassPathXmlApplicationContext方式测试bean

文章目录1 安装环境2 修改build.gradle3 添加类文件4 xml文件5 测试6 测试ClassPathXmlApplicationContext 类图技术交流 1 安装环境 《Spring源码阅读1 – Mac使用Idea工具编译Spring5源码(v5.1.6)+测试spring源码》:https://clevercode.blog.youkuaiyun.com/article/details/114232589 2 修改build.gradle dependencies { compile pr

CleverCode的博客 339

【Windows Server 2019】活动目录 (Active Directory) ——理论

博文介绍了活动目录 (Active Directory) 的定义,作用以及与 DNS 的关系。同时也介绍了AD的组织结构,包括域控制器,子域,对象,属性,容器,组织单位,域间信任等术语的解释,实验拓扑图。

VMware, Windows, Linux, Cisco, Container, Kubernetes, Kali 7752

GEE:本地影像上传到GEE的Assets中,并输入机器学习算法中作为特征变量

当我们在 Google Earth Engine(GEE)中应用机器学习算法时,会输入一些影像作为特征变量数据,进一步根据这些特征影像去推理未知区域的数据。但是 GEE 平台上计算特征变量的 API 函数并不是非常全面,我们希望获得更多的特征用于分类。这个时候,我们可以将本地计算的特征影像上传到 GEE 中进行后续处理。本文记录了从本地上传特征数据到 GEE 的 Assets 中的教程,用于后续在 GEE 中分类或者回归使用。本文也可作为《》一文的补充。

养乐多的博客 1732

利用Spring测试框架进行集成测试

一、使用Spring2.5 TestContext框架参阅:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-test/       Spring 2.5 TestContext 测试框架,是一个全新的基于注解的测试框架,为 Spring 推荐使用该测试框架。       Spring 2.5 相比于 Sprin

以梦为马 2315

LeRobot 入门教程(一)软件环境、部署流程及摄像头配置

🤖 LeRobot:开源机器人学习平台 摘要:LeRobot是一个基于PyTorch的开源机器人学习平台,提供模型、数据集和工具,旨在降低机器人技术门槛。平台包含经过验证的模仿学习和强化学习方法,并提供预训练模型、人类演示数据集及模拟环境。安装需从源码获取,支持Python 3.10环境,可选扩展包括仿真环境(如gym-aloha)、电机控制(Dynamixel/Feetech SDK)和实验跟踪(Weights & Biases)。该平台托管在HuggingFace,并设有社区Discord频道

2506_90492529的博客 3419

关于在 Spring 中对 DAO 进行单元测试的一些问题

@page { size: 20.999cm 29.699cm; margin-top: 2cm; margin-bottom: 2cm; margin-left: 2cm; margin-right: 2cm } table { border-collapse:collapse; border-spacing:0; empty-cells:show } td, th { verti

I SEE STARS 2714

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

二、springboot2.0和spring5---oauth2--服务器测试1

主要参考了 https://blog.youkuaiyun.com/u013034378/article/details/86622590 部分参考了 https://blog.youkuaiyun.com/lightofmiracle/article/details/79151074 前言 写这个博客得原因是最近想要将自己得api单独发布给第三方使用,但是又不想被别人滥用,所以想弄一个授权服务,但是网上关于oa...

chencaw的专栏 635

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

使用 Spring 2.5 TestContext 测试框架

Spring 2.5 TestContext 测试框架用于测试基于 Spring 的程序,TestContext 测试框架和低版本 Spring 测试框架没有任何关系,是一个全新的基于注解的测试框架,为 Spring 推荐使用该测试框架。概述Spring 2.5 相比于 Spring 2.0 所新增的最重要的功能可以归结为以下 3 点:基于注解的 IoC 功能; 基于注解驱动的 Spring MVC 功能; 基于注解的 TestContext 测试框架。 Spring 推荐开发者使用新的基于注解的 Test

先相信你自己,然后别人才会相信你。 1864

Spock 1.2 –轻松进行集成测试中的Spring Bean模拟

探索如何使用Spock 1.2将Spock的模拟和间谍自动注入到Spring上下文中。 Spock中的存根/模拟/间谍(及其生命周期)一直与Spock Specification类紧密结合。 只能在测试类中创建它们。 因此,使用共享的预定义模拟(在单元测试和集成测试中)是有问题的。 这种情况在Spock 1.1中有所改善,但只有在基于Spring的集成测试中使用Spock模拟子系统的...

最佳 Java 编程 776

`@DynamicPropertySource` 是 Spring Framework 5.2.5(及 Spring Boot 2.2.6)中引入的一个重要测试增强注解

| **加载时机** | ✅ 在 `ApplicationContext` **刷新前**(`prepareTestInstance` 阶段)加载,但**早于 `@DynamicPropertySource`**;属性以静态方式解析(如读取 classpath 文件或内联 `properties` 字符串)。 | ✅ **更早且更底层**:在 `SpringBootTestContextBootstrapper` 的 `getMergedContextConfiguration()` 后、`ContextL

[Blog][Domain] programb.blog.youkuaiyun.com 434

Spring-Boot -- RESTFUL风格的API接口的三种调用方式【测试】

pring-Boot -- RESTFUL风格的API接口的三种调用方式【测试】 2017年12月22日 11:09:27 阅读数:4649   项目目录结构图:     说明:博主所有基于学习的内容全部基于上图中的Spring-Boot框架,项目暂时不会上传,因为还在完善,而且内容都比较离散,都是学习中遇到哪个知识点就往里面加,因此,我只拆分其中的某个知识点出来进行博文更新。 ...

大啵鱼 3162
上一篇: ext 带复选框的树
下一篇: 重装系统
ezscript
博客等级 码龄12年 1粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值