Skip to content

Tag: junit

Comparing arrays in JUnit assertions, concise built-in way?

Is there a concise, built-in way to do equals assertions on two like-typed arrays in JUnit? By default (at least in JUnit 4) it seems to do an instance compare on the array object itself. EG, doesn’t work: Of course, I can do it manually with: ..but is there a better way? Answer Use org.junit.Assert’s method assertArrayEquals: If this method

HowTo Unit Test Client Server Code

I’m currently writing a Java Client Server Application. So i want to implement two Libraries, one for the Client and one for the Server. The Client Server Communication has a very strict protocol, that I wan’t to test with JUnit. As build tool im using Maven and a Husdon Server for continues Integration. Actually I do not have any good

Bringing unit testing to an existing project

I’m working on an existing Java EE project with various maven modules that are developed in Eclipse, bundled together and deployed on JBoss using Java 1.6. I have the opportunity to prepare any framework and document how unit testing should be brought to the project. Can you offer any advice on… JUnit is where I expect to start, is this

log4j vs. System.out.println – logger advantages?

I’m using log4j for the first time in a project. A fellow programmer told me that using System.out.println is considered a bad style and that log4j is something like standard for logging matters nowadays. We do lots of JUnit testing – System.out stuff turns out to be harder to test. Therefore I began utilizing log4j for a Console controller class,

Compare Date objects with different levels of precision

I have a JUnit test that fails because the milliseconds are different. In this case I don’t care about the milliseconds. How can I change the precision of the assert to ignore milliseconds (or any precision I would like it set to)? Example of a failing assert that I would like to pass: Answer Use a DateFormat object with a

JUnit: @Before only for some test methods?

I have some common set up code that I’ve factored out to a method marked with @Before. However, it is not necessary for all this code to run for every single test. Is there a way to mark it so the @Before method only runs before certain tests? Answer Just move out the tests that don’t need the setup code