junit

Spring的新注解

扶醉桌前 提交于 2020-02-02 17:33:07
在学习了bean配置的注解方式后 发现有两个问题 1.对于实现类中每次调用不同的Dao中的服务 都有很多重复的代码 2.解决了bean的注解配置还有一些扫描包 和 数据源等配置怎么用注解配置呢 < context:component-scan >的注解配置 先要在类里面注释@Configuration,申明该类是个配置类,即取代了config.xml 在注释@ComponentScan,取代了< context:component-scan > 注意:@ComponentScan(value={ xxx },{xxx})括号里面是个数组 如果要扫描多个包 是要用{ }的,只扫描一个就可以省略 还有可以省略value/basePackages 这时就可以去除掉config.xml的< context:component-scan > 数据库的注解配置 观察数据库的配置格式,是很类似一个普通类的配置(类似Dap/Service),所以要配置的话,可以用构造方法来配置,只是上面有参数(ref是引用类型,要注入数据源) 下面的没有参数 如果只是单纯的创建这个方法 是和原来的配置是不同的 配置文件一旦配置 不仅会根据全限定类名找到对应的QueryRunner类,注入dataSource,而且还会把这个QueryRunner实例化放入Spring的IOC容器

Using maven to create junit xml reports of jar

痞子三分冷 提交于 2020-02-02 14:30:33
问题 I have inherited a codebase whereby we have a maven project of component tests that use junit as the framework that runs the tests. Everything runs OK, but the problem is that these tests must be compiled into a jar-with-dependencies so that they can be run on a standalone computer with very limited outwards connectivity (e.g does not have access to maven central). I have managed to create a runnable jar with dependencies which I can run using junit.jar and the command line like so: java -cp

Spring @transaction not working as expected in junit in non debug mode

妖精的绣舞 提交于 2020-02-02 02:14:07
问题 All MyService methods are transactional. The junit test below, gets count of items, saves a new item, and gets count of items to make sure that counts has been incremented by 1. public class MyTest extends ServiceTest{ 1. int countBefore = myService.getCount(); //return n 2. myService.add(item); //item is really added to DB 3. int countAfter = myService.getCount(); //return n (sometimes n+1) } @Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.READ_COMMITTED) getCount(){

Testing graphics generation with JUnit

淺唱寂寞╮ 提交于 2020-02-01 22:33:06
问题 I'm using Java's Graphics2D to generate a graphical representation of a graph. I'm also using ImageIO to write a PNG file. ( ImageIO.write(image, "png", out); ) I'm wondering how should I write JUnit tests to test whether the generated graphics is what is expected. I could pre-generate the PNG files but what if the font is a bit different on a different machine? 回答1: You could try testing for specific, known features of the output e.g.: It there a white pixel at (100,100)? It the border

单元测试

左心房为你撑大大i 提交于 2020-02-01 20:50:50
在module的build.gradle添加依赖 testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' __androidTest/java__运行在jvm上的单元测试,需要连接安卓设备 __test/java__是Junit单元测试,不需要Android依赖的单元测试类,速度快,适合对java代码功能进行单元测试 添加单元测试类 来源: CSDN 作者: why123wh 链接: https://blog.csdn.net/why123wh/article/details/104136049

SpringRunner vs SpringBootTest

别来无恙 提交于 2020-02-01 05:08:25
问题 In unit test, what are the differences between @Runwith(SpringRunner.class) & @SpringBootTest ? Can you explain to me the use cases of each one? 回答1: @RunWith(SpringRunner.class) : You need this annotation to just enable spring boot features like @Autowire , @MockBean etc.. during junit testing is used to provide a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in our JUnit tests, this annotation will be required. @SpringBootTest :

How to run external testcase(Class,junit) in java program?

牧云@^-^@ 提交于 2020-02-01 03:28:36
问题 How to run external testcase(Class,junit) in java program? 回答1: If you want to run JUnit tests through a Java program, you could use the JUnitCore class JUnitCore is a facade for running tests. It supports running JUnit 4 tests, JUnit 3.8.x tests, and mixtures. To run tests from the command line, run: (windows) java -cp /path/to/junit.jar;/path/to/yourTextClasses org.junit.runner.JUnitCore TestClass1 TestClass2 .... (Unix) java -cp /path/to/junit.jar:/path/to/yourTextClasses org.junit.runner

Junit整合log4j

情到浓时终转凉″ 提交于 2020-01-31 23:58:39
今天遇到一个Spring Junit整合Log4j的小例子,控制台报了一个警告如下: log4j:WARN No ppenders could be found for logger(org.springframework.test.context.junit4.SpringJUnit4ClassRunner). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 这里的意思就是使用Junit的没时候没有加载log4j.properties配置文件。 我用的是maven创建的工程,所以我就在test测试目录下写了一个Junit4ClassRunner类,并继承了SpringJunit4ClassRunner这个类,使用静态代码块优先加载log4j配置文件。 import org.apache.log4j.PropertyConfigurator; import org.junit.runners.model.InitializationError; import org.springframework.test.context.junit4

springboot使用junit5/junit4

孤街醉人 提交于 2020-01-31 20:33:25
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage 其中 JUnit平台,其主要作用是在JVM上启动测试框架。它定义了一个抽象的TestEngineAPI来定义运行在平台上的测试框架,同时还支持通过命令行、Gradle和Maven来运行平台。 JUnit Jupiter,包含了JUnit5最新的编程模型和扩展机制。 JUnit Vintage,允许在平台上运行JUnit3和JUnit4的测试用例。 JUnit5对Java运行环境的最低要求是Java8,同时也兼容测试旧版本JDK编译出来的代码。 完整依赖: <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.5.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency>

Spring MVC Controllers Unit Test not calling @ControllerAdvice

自作多情 提交于 2020-01-30 15:22:10
问题 I have a set of Controllers in the application and a class annotated as @ControllerAdvice which sets up certain data elements that are used in each of these controllers. I'm using Spring MVC 3.2 and have Junits for these controllers. When I run the Junit the control is not going to the ControllerAdvice class wheres it works fine if I deploy the app in Tomcat and submit a request through browser. Any thoughts please?. 回答1: After using the answer from @eugene-to and another similar one here I