I think I'm having trouble with using parameters correctly in the JUnit assert in my Test method. My assertion fails even though I'm pretty sure my logic checks out.

How do I construct the correct test with assert? Any pointers appreciated :)

public String season(int month, boolean inNorthernHemisphere) {
		// For northern hemisphere
		// 1,2,12 "Winter"
		// 3,4,5 "Spring"
		// 6,7,8 "Summer"
		// 9,10,11 "Fall"
		// inNorthernHemisphere represents northern Hemisphere when true
		String season = "";
		if (inNorthernHemisphere) {
			if (month < 3 || month == 12)
				season = "Winter";
			else if (month < 6)
				season = "Spring";
			else if (month < 9)
				season = "Summer";
			else
				season = "Fall";
		} else {
			if (month < 3 || month == 12)
				season = "Summer";
			else if (month < 6)
				season = "Fall";
			else if (month < 9)
				season = "Winter";
			else
				season = "Spring";
		}
		return season;
	}

	@Test
        //Not correct assertTrue format?
	public void testSeason() {
		ControlFun cf = new ControlFun();
		boolean north = true;
		boolean south = false;
		assertTrue("Winter" == cf.season(2, north)));
		assertTrue("Winter" == cf.season(9, south)));

	}

Recommended Answers

All 3 Replies

What happens when you go to run it? Is it a logic error (you get a result, but it's not the one you think it should be) or is it a failure to compile (what does the gobbeldygook from the compiler look like?) or a run-time failure (throws an exception of some sort - what sort?)

Using Eclipse Helios BTW.

You know what? Minutes after I posted this I figured it out. I went with assert equals instead but all my tests passed. Green bars!

No runtime or compiling errors. It was just that I was sure my logic was right and my tests through JUnit kept giving me the red bars(assertion failed).

Just learning about JUnit this week and still getting familiar with it. Had a moment of frustration. All is well for now.

Thanks for the reply Jon.

@Test
	public void testSeason() {
		ControlFun cf = new ControlFun();
		boolean north = true;
		boolean south = false;
		assertEquals("Winter", cf.season(1, north));
		assertEquals("Winter", cf.season(2, north));
		assertEquals("Spring", cf.season(3, north));
		assertEquals("Spring", cf.season(4, north));
		assertEquals("Spring", cf.season(5, north));
		assertEquals("Summer", cf.season(6, north));
		assertEquals("Summer", cf.season(7, north));
		assertEquals("Summer", cf.season(8, north));
		assertEquals("Fall", cf.season(9, north));
		assertEquals("Fall", cf.season(10, north));
		assertEquals("Fall", cf.season(11, north));
		assertEquals("Winter", cf.season(12, north));

		assertEquals("Summer", cf.season(1, south));
		assertEquals("Summer", cf.season(2, south));
		assertEquals("Fall", cf.season(3, south));
		assertEquals("Fall", cf.season(4, south));
		assertEquals("Fall", cf.season(5, south));
		assertEquals("Winter", cf.season(6, south));
		assertEquals("Winter", cf.season(7, south));
		assertEquals("Winter", cf.season(8, south));
		assertEquals("Spring", cf.season(9, south));
		assertEquals("Spring", cf.season(10, south));
		assertEquals("Spring", cf.season(11, south));
		assertEquals("Summer", cf.season(12, south));
	}

Excellent. Glad to help, even happier when you figure it out on your own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.