I have a Gradle project with the following structure: Now I want to add a unit test which uses a resource (either “raw” or “asset”). I put my resource into project/androidTest/assets/test_file and access it with getContext().getResources().getAssets().open(“test_file”); (in an AndroidTestCase). However, this gives me a FileNotFoundException. How can I fix this? Answer It looks like you’re trying to create an instrumented
Tag: unit-testing
Unit testing of a shuffle method on a list
Consider the following class: How would I unit test the shuffle() method? I am using JUnit 4 for testing. I have the following options: Test shuffle() to see that it does not generate an exception. Test shuffle() and check if the deck actually gets shuffled. Example pseudocode of option 2: The only culprit here is that when executing a test
How to enable debug on my JUnit through Gradle test task
I get into trouble while I try to run my JUnit test through Gradle test task. While I run the test in eclipse directly with Run As -> JUnit test, everything is ok, the test succeeds. But through test task, test always fails. Probably some trouble with the encoding of my resource txt file. So I would like to enable
Spock throw exception test
I test Java code with Spock. I test this code: I wrote test: And it fails because AnotherCustomExceptio is thrown. But in the try{}catch block I catch this exception and throw a CustomException so I expected that my method will throw CustomException and not AnotherCustomExceptio. How do I test it? Answer I believe your then block needs to be fixed.
Mocking static methods with Mockito
I’ve written a factory to produce java.sql.Connection objects: I’d like to validate the parameters passed to DriverManager.getConnection, but I don’t know how to mock a static method. I’m using JUnit 4 and Mockito for my test cases. Is there a good way to mock/verify this specific use-case? Answer Use PowerMockito on top of Mockito. Example code: More information: Why doesn’t
Compare two XML strings ignoring element order
Suppose I have two xml strings How to write a test that compares those two strings and ignores the element order? I want the test to be as short as possible, no place for 10-line XML parsing etc. I’m looking for a simple assertion or something similar. I have this (which doesn’t work) Answer My original answer is outdated. If
JUnit Tests for GUI in Java
I would like to write test cases for a GUI. I want to know how do you simulate a click of JButton, or how do you extract the elements of a JTable. For the purpose of this, I have built a simple GUI that increase the count by 1 if the button is clicked and the JTextfield is empty, but
Mocking a Spy method with Mockito
I am writing a unit test for a FizzConfigurator class that looks like: I’d like to write a simple unit test that stubs the doWidget(String,Config) method (so that it doesn’t actually fire and hit the database), but that allows me to verify that calling doBuzz(String) ends up executing doWidget. Mockito seems like the right tool for the job here. This
passing Parameterized input using Mockitos
I am using Mockito for unit testing. I am wondering if its possible to send Parametrized input parameters with as in Junit testing e.g Answer In JUnit, Parameterized tests use a special runner that ensure that the test is instantiated multiple times, so each test method is called multiple times. Mockito is a tool for writing specific unit tests, so
IntelliJ – Failed to start: 0 passed, 1 not started
Just been playing around for the first time with IntelliJ IDEA Community edition, first time I have worked with it so if I’m missing something, please excuse me. I have a bunch of unit tests which I run, however, when running them in IntelliJ (with the standard setup out of the box), I intermittently get the following error in the