Skip to content

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 he…

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 be…

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-…

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 or…

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. Mo…

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 …