Skip to content

Tag: junit

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…

Comparing dates with JUnit testing

Hello I’m new to the site and have a issue with my application using JUnit testing. My issue is when I try to compare the Date method with itself it always fails. I printed the Date object in the test to see the problem and always end up with the package name and random letters. Here is the Date constru…

How to assert greater than using JUnit Assert?

I have these values coming from a test and I try I get the java.lang.AssertionError and detailMessage on debugging is null. How can I assert greater than conditions in using JUnit Answer Just how you’ve done it. assertTrue(boolean) also has an overload assertTrue(String, boolean) where the String is the…

Junit Testing JNDI InitialContext outside the application server

Please help me to mock the above code. Hi Tom Anderson I tried the below code But its giving error as: Please help me to test the code i just want connection from JNDI datasource Answer The orthodox thing to do here would be to change you code so that the Context is injected into it (by a dependency injection

Mockito mock objects returns null

I try to implement some tests for my JSF application and for the mocks I am using mockito. (I also use spring) The test succeeds, but when I want to retrieve the instance with the getInstance method. All Parameters which I have set before (via the constructor before) are null. I am new to mocked objects, so i…

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…