Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test passing of time in jUnit test without accessing private variables?
    primarykey
    data
    text
    <p>I'm unit testing a class where I need a certain amount of time to pass before I can check results. Specifically I need x minutes to pass before I can tell whether the test worked or not. I have read that in unit testing we should be testing the interface and not the implementation, so we should not be accessing private variables, but other than putting a sleep in my unit test I don't know how to test without modifying private variables.</p> <p>My test is set up like this:</p> <pre><code>@Test public void testClearSession() { final int timeout = 1; final String sessionId = "test"; sessionMgr.setTimeout(timeout); try { sessionMgr.createSession(sessionId); } catch (Exception e) { e.printStackTrace(); } DBSession session = sessionMgr.getSession(sessionId); sessionMgr.clearSessions(); assertNotNull(sessionMgr.getSession(sessionId)); Calendar accessTime = Calendar.getInstance(); accessTime.add(Calendar.MINUTE, - timeout - 1); session.setAccessTime(accessTime.getTime()); // MODIFY PRIVATE VARIABLE VIA PROTECTED SETTER sessionMgr.clearSessions(); assertNull(sessionMgr.getSession(sessionId)); } </code></pre> <p>Is it possible to test this other than modifying the accessTime private variable (via creating the setAccessTime setter or reflection), or inserting a sleep in the unit test?</p> <p>EDIT 11-April-2012</p> <p>I am specifically trying to test that my SessionManager object clears sessions after a specific period of time has passed. The database I am connecting to will drop connections after a fixed period of time. When I get close to that timeout, the SessionManager object will clear the sessions by calling a "finalise session" procedure on the database, and removing the sessions from it's internal list.</p> <p>The SessionManager object is designed to be run in a separate thread. The code I am testing looks like this:</p> <pre><code>public synchronized void clearSessions() { log.debug("clearSessions()"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, - timeout); Iterator&lt;Entry&lt;String, DBSession&gt;&gt; entries = sessionList.entrySet().iterator(); while (entries.hasNext()) { Entry&lt;String, DBSession&gt; entry = entries.next(); DBSession session = entry.getValue(); if (session.getAccessTime().before(cal.getTime())) { // close connection try { connMgr.closeconn(session.getConnection(), entry.getKey()); } catch (Exception e) { e.printStackTrace(); } entries.remove(); } } } </code></pre> <p>The call to connMgr (ConnectionManager object) is a bit convoluted, but I am in the process of refactoring legacy code and it is what it is at the moment. The Session object stores a connection to the database as well as some associated data.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload