I tried using TestNG

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.

Preparation

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.

Make a test case

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 ...

Make a test suite

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

I tried using TestNG
I tried using Gson
I tried using Galasa
I tried using azure cloud-init
I tried using Apache Wicket
I tried using Java REPL
I tried using anakia + Jing now
I tried using Spring + Mybatis + DbUnit
I tried using JOOQ with Gradle
I tried using Java8 Stream API
I tried using JWT in Java
[Android] I tried using Coordinator Layout.
I tried using Pari gp container
I tried using WebAssembly Stadio (2018/4/17 version)
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring â‘ 
I tried FizzBuzz.
I tried JHipster 5.1
I tried using Elasticsearch API in Java
I tried using Realm with Swift UI
I tried using Java's diagnostic tool Arthas
I tried using UICollectionViewListCell added from Xcode12.
I tried using Scalar DL with Docker
I tried using OnlineConverter with SpringBoot + JODConverter
It's new, but I tried using Groonga
I tried using OpenCV with Java + Tomcat
I tried using Docker for the first time
[I tried] Spring tutorial
I tried using Junit on Mac VScode Maven
[For beginners] I tried using DBUnit in Eclipse
I tried barcode scanning using Rails + React + QuaggaJS
I tried running Autoware
[For beginners] I tried using JUnit 5 in Eclipse
[Android] I quit SQLite and tried using Realm
I tried QUARKUS immediately
I made blackjack with Ruby (I tried using minitest)
I tried Spring Batch
[API] I tried using the zip code search API
I tried node-jt400 (Programs)
I tried node-jt400 (execute)
I tried to implement a server using Netty
I tried using the profiler of IntelliJ IDEA
I tried node-jt400 (Transactions)
I tried using a database connection in Android development
I tried using the Server Push function of Servlet 4.0
I tried using Alibaba Cloud's KMS (Key Management Service) service
I tried using Google Cloud Vision API in Java
I tried to operate SQS using AWS Java SDK
I tried using the Migration Toolkit for Application Binaries
I tried using Log4j2 on a Java EE server
I tried using YOLO v4 on Ubuntu and ROS
I tried using Docker Desktop for Windows on Windows 10 Home
I tried using an extended for statement in Java
I tried scraping a stock chart using Java (Jsoup)
I tried to build an environment using Docker (beginner)
I tried node-jt400 (Environment construction)
I tried DI with Ruby