I was on leave when I was on fire due to the lack of Java6 support, and I escaped the difficulty. I wonder what happened to JUnit3 or the test class of djUnit (it seems to work for the time being).
That's why I tried using TestNG even now.
With Maven, add the following dependencies and you're ready to go.
pom.xml (copy and paste strictly prohibited)
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
I have hardly used Maven itself, but if I copy and rewrite the description of pom.xml, it will cause an accident with high probability. In fact, when I copied and pasted it, the scope was wrong, and although it worked, testng-6.14.3.jar was included in the archive to be deployed. Be sure to use IDE auto-completion.
There may be quite a lot of people who have used it but don't know how to write pom.xml.
This is a test class in JUnit. It's pretty similar. If you have used JUnit4, you will not be confused.
LogoutBeanTest.java
public class LogoutBeanTest {
/**Tested class*/
private LogoutBean logoutBean;
@Test
public void logout() throws Exception {
String result = logoutBean.logout();
//Verification of results
assertEquals(logoutBean.getMessage(), "logged out");
assertEquals(result, "/logout/logout.jsf");
}
@BeforeMethod
public void setUpMethod() throws Exception {
logoutBean = new LogoutBean();
}
}
I was a little worried that the order of the arguments of ʻassertEquals ()` is the reverse of JUnit. In JUnit, the expected result (expected) comes first, but in TestNG, the actual result (actual) comes first. There were quite a few people who wrote in JUnit on the contrary ...
You can manage multiple test classes in XML. When executing TestNG from the command line, it is convenient to be able to call multiple test classes at once, but I have not found the advantage in the IDE.
testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="cc.cress.sample">
<test name="Login">
<classes>
<class name="cc.cress.sample.login.LoginBeanTest"/>
<class name="cc.cress.sample.util.PasswordEncrypterTest"/>
</classes>
</test>
<test name="Logout">
<classes>
<class name="cc.cress.sample.logout.LogoutBeanTest"/>
</classes>
</test>
</suite>
After that, I introduced PowerMockito (mock object) and Jacoco (coverage acquisition), but it has become quite volumey, so that's it.
Recommended Posts