01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

making the most <strong>of</strong> a framework<br />

Automate your tests with a testing framework<br />

Let’s take a simple test case and automate it using JUnit. JUnit provides common<br />

resources and behaviors you need for your tests, and then invokes each <strong>of</strong> your tests,<br />

one at a time. JUnit gives you a nice GUI to see your tests run, as well, but that’s really<br />

a small thing compared to the power <strong>of</strong> automating your tests.<br />

Here’s a static<br />

final <strong>of</strong> empty<br />

checkboxes that can<br />

be used in several<br />

different tests.<br />

JUnit calls setUp()<br />

before each test is<br />

run, so here’s where to<br />

initialize variables used<br />

in the test methods.<br />

tearDown() is for<br />

cleaning up. JUnit calls<br />

this method when each<br />

test is finished.<br />

Here’s an actual<br />

test. You annotate it<br />

with @Test so JUnit<br />

knows it’s a test<br />

and can run it. The<br />

method just sends a<br />

test message and a<br />

checkboxState.<br />

250 Chapter 7<br />

You’ve got to import the JUnit classes.<br />

}<br />

package headfirst.sd.chapter7;<br />

import java.io.*;<br />

import java.net.Socket;<br />

import org.junit.*;<br />

public class TestRemoteReader {<br />

private Socket mTestSocket;<br />

private ObjectOutputStream mOutStream;<br />

private ObjectInputStream mInStream;<br />

These are objects used in several<br />

<strong>of</strong> the test cases.<br />

public static final boolean[] EMPTY_CHECKBOXES = new boolean[256];<br />

@Before<br />

public void setUp() throws IOException {<br />

mTestSocket = new Socket(“127.0.0.1”, 4242);<br />

mOutStream =<br />

new ObjectOutputStream(mTestSocket.getOutputStream());<br />

mInStream =<br />

new ObjectInputStream(mTestSocket.getInputStream());<br />

}<br />

@After<br />

public void tearDown() throws IOException {<br />

mTestSocket.close();<br />

mOutStream = null;<br />

mInStream = null;<br />

mTestSocket = null;<br />

}<br />

Since these are<br />

annotated with @Before<br />

and @After, they’ll get<br />

called by JUnit before<br />

and after each test.<br />

@Test<br />

public void testNormalMessage()throws IOException {<br />

boolean[] checkboxState = new boolean[256];<br />

checkboxState[0] = true;<br />

checkboxState[5] = true;<br />

checkboxState[19] = true;<br />

mOutStream.writeObject(“This is a test message!”);<br />

mOutStream.writeObject(checkboxState);<br />

}<br />

You can use mOutStream because it<br />

was set up in the setup() method<br />

that JUnit will already have called.<br />

Download at WoweBook.Com<br />

JUnit also has a textbased<br />

test runner<br />

and plug-ins for most<br />

popular IDEs.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!