junit

Creating intent in test: “Method putExtra in android.content.Intent not mocked”

心已入冬 提交于 2020-05-30 07:19:22
问题 I'm trying to unit test a broadcast receiver which listens for "com.android.music.metachanged" intents using JUnit4 and Mockito. The broadcast receiver starts a service when it receives an intent. I want to assert that the service is started. I also want to assert that the string extra "artist" of the received intent is the same as the one of the sent intent. This is what I have so far... @RunWith(PowerMockRunner.class) public class MusicBroadcastReceiverUnitTest { private

@InjectMocks, the constructor or the initialization block threw an exception

蓝咒 提交于 2020-05-29 03:55:38
问题 When I use @InjectMocks , an exception was occurred. My code is shown below: class A { private X x; private Y y; public A(String ip, int port) { this(someMethodCall(ip, port)); // } private A(X x) { this.x = x; this.y = new Y(); } } UT: public class ATest() { @InjectMocks A a; @Mock X x; @Mock Y y; @Test ... } it will throw an NPE, can someone help me? org.mockito.exceptions.base.MockitoException: Cannot instantiate @InjectMocks field named 'channel' of type 'class Juinit3.Channel'. You haven

JUnit 5 — global timeout?

烈酒焚心 提交于 2020-05-29 03:30:19
问题 In JUnit 5, what is the best way to enforce a global timeout for all tests? I see that JUnit 4 has a @Rule feature, but this has been removed in JUnit 5. And I would prefer not to have to copy-paste assertTimeoutPreemptively for every test. 回答1: You mentioned the JUnit 4 Timeout @Rule, which allows specifying timeouts on a per-test class basis. JUnit 5.5 has a direct equivalent as an experimental feature in the @Timeout annotation. This can be applied to the class level in the same manner as

JUnit 5 — global timeout?

两盒软妹~` 提交于 2020-05-29 03:28:41
问题 In JUnit 5, what is the best way to enforce a global timeout for all tests? I see that JUnit 4 has a @Rule feature, but this has been removed in JUnit 5. And I would prefer not to have to copy-paste assertTimeoutPreemptively for every test. 回答1: You mentioned the JUnit 4 Timeout @Rule, which allows specifying timeouts on a per-test class basis. JUnit 5.5 has a direct equivalent as an experimental feature in the @Timeout annotation. This can be applied to the class level in the same manner as

JUnit 5 — global timeout?

萝らか妹 提交于 2020-05-29 03:28:23
问题 In JUnit 5, what is the best way to enforce a global timeout for all tests? I see that JUnit 4 has a @Rule feature, but this has been removed in JUnit 5. And I would prefer not to have to copy-paste assertTimeoutPreemptively for every test. 回答1: You mentioned the JUnit 4 Timeout @Rule, which allows specifying timeouts on a per-test class basis. JUnit 5.5 has a direct equivalent as an experimental feature in the @Timeout annotation. This can be applied to the class level in the same manner as

Spring's @Retryable not working when running JUnit Test

女生的网名这么多〃 提交于 2020-05-29 03:24:49
问题 I have this test: @RunWith(MockitoJUnitRunner.class) public class myServiceTest { @InjectMocks myService subject; private myService spy; @Before public void before() { spy = spy(subject); } @Test public void testing() { when(spy.print2()).thenThrow(new RuntimeException()).thenThrow(new RuntimeException()).thenReturn("completed"); spy.print1(); verify(spy, times(3)).print2(); } and then I have: @Service("myService") public class myService extends myAbstractServiceClass { public String print1()

Spring's @Retryable not working when running JUnit Test

霸气de小男生 提交于 2020-05-29 03:23:53
问题 I have this test: @RunWith(MockitoJUnitRunner.class) public class myServiceTest { @InjectMocks myService subject; private myService spy; @Before public void before() { spy = spy(subject); } @Test public void testing() { when(spy.print2()).thenThrow(new RuntimeException()).thenThrow(new RuntimeException()).thenReturn("completed"); spy.print1(); verify(spy, times(3)).print2(); } and then I have: @Service("myService") public class myService extends myAbstractServiceClass { public String print1()

Spring's @Retryable not working when running JUnit Test

笑着哭i 提交于 2020-05-29 03:22:21
问题 I have this test: @RunWith(MockitoJUnitRunner.class) public class myServiceTest { @InjectMocks myService subject; private myService spy; @Before public void before() { spy = spy(subject); } @Test public void testing() { when(spy.print2()).thenThrow(new RuntimeException()).thenThrow(new RuntimeException()).thenReturn("completed"); spy.print1(); verify(spy, times(3)).print2(); } and then I have: @Service("myService") public class myService extends myAbstractServiceClass { public String print1()

Surefire Maven plugin: “Corrupted STDOUT by directly writing to native stream in forked JVM”

断了今生、忘了曾经 提交于 2020-05-24 20:09:04
问题 My JUnit tests are failing when running them through Maven and the Surefire plugin (version information below). I see the error message: Corrupted STDOUT by directly writing to native stream in forked JVM 4. See FAQ web page and the dump file C:\(...)\target\surefire-reports\2019-03-20T18-57-17_082-jvmRun4.dumpstream The FAQ page points out some possible reasons but I don't see how I can use this information to start solving this problem: Corrupted STDOUT by directly writing to native stream

How can I test a micronaut Service with junit5?

三世轮回 提交于 2020-05-24 05:14:30
问题 I tried to run the mathService example presented under: https://micronaut-projects.github.io/micronaut-test/latest/guide/#junit5 which should explain testing a micronaut service with junit5. I added testAnnotationProcessor "io.micronaut:micronaut-inject-java" testCompile "io.micronaut.test:micronaut-test-junit5:1.1.3" testCompile "org.mockito:mockito-junit-jupiter:2.22.0" testRuntime "org.junit.jupiter:junit-jupiter-engine:5.1.0" to my dependencies. I coded MathService, MathServiceImpl in src