Code execution + failure rerun of TestNg of SpringBoot

Keywords: Java Spring Spring Boot unit testing

Code execution + failure rerun of TestNg of SpringBoot

My design idea is to directly add the corresponding Controller layer and service layer in SpringBoot, and directly encapsulate the run and failure rerun into interfaces. In fact, the Controller layer to realize the call is only a way and an exit. The important thing is the design of the implementation layer, which is blood and meat.

Design ideas

1. Implement an interface that runs all use cases
2. Implement an interface for running failure cases
3. Now that we have executed the use case and want to see the results, we will receive the results by mail

Mail sending function

Add JavaMail API

Address: Portal
Add it to the pom file

Start smtp service on QQ





Remember this authorization code

Realize the function of sending mail

This paragraph is the function of the boss I copied. I just put it in SpringBoot

package com.newcrud.utils;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

@Component
public class SendMail {
    public void sendMail(){

        // Create a Property file object
        Properties props = new Properties();

        // Set the information of the mail server. Set the smtp host name here
        props.put("mail.smtp.host", "smtp.qq.com");

        // Set the port of socket factory
        props.put("mail.smtp.socketFactory.port", "465");

        // Set socket factory
        props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

        // Setting requires authentication
        props.put("mail.smtp.auth", "true");

        // Set the SMTP port. The SMTP port of QQ is 25
        props.put("mail.smtp.port", "25");

        // Authentication implementation
        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                // The second parameter is the authorization code for QQ to open smtp
                return new PasswordAuthentication("xxxxx@qq.com", "indwjcbpfwrbbebf");
            }
        });
        try {

            // Create an instance object of the MimeMessage class
            Message message = new MimeMessage(session);

            // Set sender email address
            message.setFrom(new InternetAddress("xxxx@qq.com"));

            // Set recipient email address
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xxx@xxx.com"));

            // Set message subject
            message.setSubject("Unit test send mail");

            // Create an object of MimeBodyPart to add content
            BodyPart messageBodyPart1 = new MimeBodyPart();

            // Set message body content
            messageBodyPart1.setText("This is the body of the message");
            // Create another MimeBodyPart object to add additional content
            MimeBodyPart messageBodyPart2 = new MimeBodyPart();

            // Set the path of the attachment file in the message
            String filename = "test-output/emailable-report.html";

            // Create a datasource object and pass the file
            DataSource source = new FileDataSource(filename);

            // Set handler
            messageBodyPart2.setDataHandler(new DataHandler(source));

            // load file
            messageBodyPart2.setFileName(filename);

            // Create an instance object of the MimeMultipart class
            Multipart multipart = new MimeMultipart();

            // Add body 1 content
            multipart.addBodyPart(messageBodyPart1);

            // Add body 2 content
            multipart.addBodyPart(messageBodyPart2);

            // Set content
            message.setContent(multipart);
            // Final send mail
            Transport.send(message);

            System.out.println("=====The message has been sent=====");

        } catch (MessagingException e) {

            throw new RuntimeException(e);

        }

    }
}

Let's test whether the function is normal. I have executed the xml file once and generated the corresponding out put folder

package com.newcrud.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
@SpringBootTest
public class SendMailTest extends AbstractTestNGSpringContextTests {
    @Autowired
    SendMail sendMail;

    @Test
    public void testSendMail() {
        sendMail.sendMail();
    }
}

After execution, the mail is received normally, indicating that the function is normal.

Implement all and failed test cases

Interface

package com.newcrud.service;

public interface TestNgRunService {
    void runAll();
    void runFail();
}

Implementation class

package com.newcrud.service.impl;

import com.newcrud.service.TestNgRunService;
import org.apache.tomcat.jni.Time;
import org.springframework.stereotype.Service;
import org.testng.TestNG;

import java.util.ArrayList;
import java.util.List;

@Service
public class TestNgRunServiceImpl implements TestNgRunService {
    @Override
    public void runAll() {
        TestNG testNone=new TestNG();
        List<String> list=new ArrayList<>();
        list.add("testng.xml");
        testNone.setTestSuites(list);
        testNone.run();
    }

    @Override
    public void runFail() {
        TestNG testNTwo = new TestNG();
        List<String> list=new ArrayList<>();
        list.add("test-output/testng-failed.xml");
        testNTwo.setTestSuites(list);
        testNTwo.run();
    }
}

Execution class

package com.newcrud.service.impl;

import com.newcrud.utils.SendMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;

import static org.testng.Assert.*;

@SpringBootTest
public class TestNgRunServiceImplTest extends AbstractTestNGSpringContextTests {
    @Autowired
    TestNgRunServiceImpl testNgRunService;
    @Autowired
    SendMail sendMail;
    @Test
    public void testRunAll() {
        testNgRunService.runAll();
        sendMail.sendMail();
    }

    @Test
    public void testRunFail() {
        testNgRunService.runFail();
        sendMail.sendMail();
    }
}

Test it

Test class A

package com.newcrud.testngTest;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestFour  {
    @Test
    public void testA()  {
        System.out.println("testA");
        Assert.assertEquals(1,2);
    }

    @Test
    public void testB(){
        System.out.println("testB");
        Assert.assertEquals(1,1);
    }
}

Test class B

package com.newcrud.testngTest;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestThree {
    @Test
    public void testA(){
        System.out.println("testA");
        Assert.assertEquals(1,2);
    }
    @Test
    public void testB(){
        System.out.println("testB");
        Assert.assertEquals(1,1);
    }
}

xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2"  name="/Users/zc/IdeaProjects/NewCRUD/src/test/java/com/newcrud" time-out="3000">
        <classes>
            <class name="com.newcrud.testngTest.TestThree"></class>
            <class name="com.newcrud.testngTest.TestFour"></class>
        </classes>
    </test>
</suite>

In the future, you can directly execute the execution class TestNgRunServiceImplTest on the IDEA. After each execution, the corresponding e-mail will be sent.

People who saw it must be very puzzled. Why did I write the implementation class @Service? Because what I want to achieve is calling through the interface. Unfortunately, I failed, so I deleted the Controller, and it was good. But after calling SpringBoot, it would be too difficult to find the corresponding test class in xml. So that's it. For the time being, I'm tired

Posted by bicho83 on Tue, 19 Oct 2021 00:29:59 -0700