Questions tagged [junit4]

Version 4 of the popular Junit Java Unit testing framework

Version 4 of the popular JUnit Java Unit testing framework.

New features are the introducing of annotations to use the framework.

Use this tag only for question related to features provided by version 4. Use for general questions.

4127 questions
23
votes
7 answers

How do I test exceptions in a parameterized test?

In JUnit4 you can write parameterized unit tests by providing parameters collection in one method, which will be passed to the constructor of the test and testing in another method. If I have a parameter for which I expect an exception to be thrown,…
Gabriel Ščerbák
  • 18,240
  • 8
  • 37
  • 52
23
votes
4 answers

How to continue test after JUnit ExpectedException if thrown?

I have set up some JUnit (4.12) test with the ExpectedException feature, and I would like the test to continue after the expected exception. But I never see the log '3', as the execution seems to stop after the exception, event if catch? Is this…
Deathtiny
  • 728
  • 3
  • 8
  • 14
23
votes
3 answers

Espresso how to wait for some time(1 hour)?

In my test case I have to record for 1 hour, in robotium solo.sleep(600000) had done my work, but In espresso I am confused with IdlingResource concept. I have to start recording and wait for some time(depending on the type of test) 15mins, 60mins…
Shivaraj Patil
  • 8,186
  • 4
  • 29
  • 56
23
votes
3 answers

Basic JUnit test for JavaFX 8

I want to create basic JUnit test for JavaFX 8 application. I have this simple code sample: public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void…
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
23
votes
3 answers

How to force a fresh version of the Spring context BEFORE the test executes

I use the SpringJUnit4ClassRunner for writing integration tests. I also use @DirtiesContext for tests that leave the context in an broken state behind and that works just fine. But now I have a test the sets an important SystemProperty in an static…
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
22
votes
2 answers

How to run all JUnit tests in a category/suite with Ant?

I'm using JUnit Categories and ClassPathSuite in a setup similar to that described in this answer. To recap: public interface FastTests…
Jonik
  • 80,077
  • 70
  • 264
  • 372
22
votes
3 answers

How do I validate that the @RequestParams are not empty?

I have a calculator service that gets the operation type, num1 and num2 from the user. I need to validate that the user actually inputs these values and doesn't just leave it blank. @RequestMapping(value = "/calculate") @ResponseBody public…
Saakina
  • 275
  • 1
  • 2
  • 9
22
votes
2 answers

Will the 'finally' block fire even after a Junit test throws an Assertion Error from with in 'try' block?

Will the writer.close() method inside the finally { } block run on an Junit Assertion Error? Assume the following code: @Test public void testWriter() { try { writer.open(); final List myBeans = new…
Hari Krishna Ganji
  • 1,647
  • 2
  • 20
  • 33
22
votes
2 answers

JerseyTest and JUnit throws NullPointerException

I have some problems with the jersey test framework. If i use the @Before and @After annotations, then the target method throws a NullPointerException. I thought JerseyTest works with JUnit? Where is my problem? Jersey: 2.12 JUnit: 4.11 Code that…
Dominic
  • 735
  • 2
  • 8
  • 20
22
votes
5 answers

Combining @ClassRule and @Rule in JUnit 4.11

In JUnit 4.10 and below, it is possible to annotate a rule as both @Rule and @ClassRule. This means the rule gets invoked before before/after the class, and before/after each test. One possible reason for doing so is to set up an expensive external…
Rowan
  • 2,585
  • 4
  • 24
  • 34
22
votes
5 answers

Java - JUnit Test Generators

I would like to know about good tools to automatically generate JUnit4 tests. By automatic generation, I mean that if I write a .java file, then a test file for the same should be created with method placeholders and setup/teardown in place (like…
FirstName LastName
  • 1,891
  • 5
  • 23
  • 37
22
votes
3 answers

Can Selenium take a screenshot on test failure with JUnit?

When my test case fails, especially on our build server, I want to take a picture / screenshot of the screen to help me debug what happened later on. I know how to take a screenshot, but I was hoping for a way in JUnit to call my takeScreenshot()…
Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108
21
votes
7 answers

Specifying order of execution in JUnit test case

I have a test case where I add an entity, update it and delete the same. Hence, the order of execution is important here. I want it to be : Create Update Delete Strangely, for just one test case ( out of 15) , JUnit executes it in the following…
crazyaboutliv
  • 3,029
  • 9
  • 33
  • 50
21
votes
2 answers

Android unit testing view model that receives flow

I have a ViewModel that talks to a use case and gets a flow back i.e Flow. I want to unit test my ViewModel. I am new to using the flow. Need help pls. Here is the viewModel below - class MyViewModel(private val handle: SavedStateHandle,…
Ma2340
  • 647
  • 2
  • 17
  • 34