junit

How to capture stdout/stderr in junit 5 gradle test report?

六眼飞鱼酱① 提交于 2020-01-24 06:43:33
问题 My gradle project uses junit 5 and I'm trying to get the test reports to show up on my build server. The XML report basically looks fine – it contains all the test classes and methods, but it is missing stdout/stderr printed in test methods. There is only some CDATA containing test metadata. @Test void testToString() { System.out.println("Hello world"); ... } XML report: <testcase name="testToString()" classname="com.my.company.PairsTest" time="0.008"> <system-out><![CDATA[ unique-id: [engine

How keep spring context loaded between test runs in Intellij Idea?

蹲街弑〆低调 提交于 2020-01-24 03:51:21
问题 Spring-test provides annotation for loading spring context during test runs. For example there is a org.springframework.test.context.junit4.SpringJUnit4ClassRunner junit runner class and org.springframework.test.context.ContextConfiguration annotation for specify context loading. For integration tests context loading can be quite long and during debugging it is needed to run same test dozen times until it "lose" all it's errors. Is there a way to do so without loading context multiple times?

开发中遇到的问题

旧时模样 提交于 2020-01-23 16:08:01
使用spring的单元测试,依赖注入时报空指针(即依赖注入失败) 我在依赖注入上打了断点它也进不去,仔细检查了是不是启动类没有放在相应的包下面,配置类没有加@Configuration或者@component注解之类。发现这些都是注意到了的。当时就感觉好玄学,这个程序像见/闹鬼了一样。后面静下来想了一下,使用了springboot的脚手架(即 使用 spring initializr 构建项目)新建了一个demo,把代码拿过来测试,竟然成功。仔细比对,发现原来是@Test测试类导的jar包不正确,导致了他没有把spring的bean注入到spring容器中.后面去官网看了一下:发现spring boot 2.2 之前使用的是 Junit4 而后续的使用的是Junit5,导致缺少包。(我用的maven创建的项目,它默认添加的依赖是junit4,springboot的版本是2.2.2,所以单元测试时导入的就是junit4的jar) import org.junit.Test; 2.2以前的版本 import org.junit.jupiter.api.Test; 2.2以后的版本 总结: 在开发过程中,我们总是会遇到一些稀奇古怪的问题,然后我们总是会去抱怨,说是开发工具的问题,更多的说为什么同样的代码在他的机器上能跑,在我的机器上就不能跑?就感觉很玄学,像是见了鬼一样

Spring AOP - @Pointcut: @Before advice for @Test methods does not work

孤人 提交于 2020-01-23 09:58:30
问题 I am working with: Spring Framework 4.3.2 AspectJ 1.8.9 JUnit Gradle The project is based in multi-modules. In src/main/java ( main ) I have some @Aspect classes and they work how is expected. I can confirm it through Runtime and Testing Now I need for JUnit through logging show the @Test method name that is executed Therefore in src/test/java ( test ) I have the following: class TestPointcut { @Pointcut("execution(@org.junit.Test * *())") public void testPointcut(){} } @Aspect @Component

Spring AOP - @Pointcut: @Before advice for @Test methods does not work

假如想象 提交于 2020-01-23 09:58:12
问题 I am working with: Spring Framework 4.3.2 AspectJ 1.8.9 JUnit Gradle The project is based in multi-modules. In src/main/java ( main ) I have some @Aspect classes and they work how is expected. I can confirm it through Runtime and Testing Now I need for JUnit through logging show the @Test method name that is executed Therefore in src/test/java ( test ) I have the following: class TestPointcut { @Pointcut("execution(@org.junit.Test * *())") public void testPointcut(){} } @Aspect @Component

Instrumented Unit Class Test - Can't create handler inside thread that has not called Looper.prepare()

隐身守侯 提交于 2020-01-23 09:05:01
问题 Sorry I've looked at tutorials all over the place and not found the answer I'm looking for. I'm following Google's Tutorial at the moment here: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html I'm trying to create an Instrumented test and when I run it I'm getting the error : java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() So my test is as follows: package testapp.silencertestapp; import android

How to simulate throwing an exception only once in retry with JUNIT/Mockito test?

[亡魂溺海] 提交于 2020-01-23 07:37:08
问题 I put a simple retry because the operation can rarely fail. The simplified code is below. The method putObject can accidentally throw an exception, in this case the retry should allow to invoke this method again. Is it possible to write a JUnit test for this? I know that with Mockito library we can force to throw an Exception invoking a method but how to force this exception to be thrown only once? public class RetryExample { Bucket bucket = new Bucket(); static int INTERNAL_EXCEPTION_CODE =

Is there a way to use junit.extensions.TestSetup for tests on Android?

☆樱花仙子☆ 提交于 2020-01-23 06:32:06
问题 The android sdk source includes source for junit.extensions, but the classes are not in android.jar even though junit.framework and junit.runner are in there. I tried creating my own junit.extensions package and using the source included in the android sdk source, but I get a ClassCastException (see below). Something in android.test.suitebuilder is trying to cast the return value from the suite() method to a TestCase, even though suite returns a Test interface. I want to use TestSetup class

Is there a way to use junit.extensions.TestSetup for tests on Android?

余生长醉 提交于 2020-01-23 06:31:06
问题 The android sdk source includes source for junit.extensions, but the classes are not in android.jar even though junit.framework and junit.runner are in there. I tried creating my own junit.extensions package and using the source included in the android sdk source, but I get a ClassCastException (see below). Something in android.test.suitebuilder is trying to cast the return value from the suite() method to a TestCase, even though suite returns a Test interface. I want to use TestSetup class

JUnit crashes saying method should be static, then crashes saying it shouldn't?

↘锁芯ラ 提交于 2020-01-23 05:49:04
问题 I'm trying to make a simple example test in JUnit that tests two things, then does teardown. import org.junit.*; public class TestFoobar { @Test public void testOneThing() { // Code that tests one thing } @Test public void testAnotherThing() { // Code that tests another thing } @AfterClass @Test public void tearDownClass() throws Exception { // Code executed after the last test method } } When I try to run this, it crashes saying tearDownClass has to be static: $ javac TestFoobar.java -cp