All Downloads are FREE. Search and download functionalities are using the official Maven repository.

templates.util.ObjectUtilImplTest.ftl Maven / Gradle / Ivy

Go to download

Maven plugin to generate Java classes from StoredProcedure and Functions in Database

There is a newer version: 1.8.20
Show newest version
package ${javaPackage}.${utilFolderName};
<#assign importList = ["java.sql.Connection", "java.sql.SQLException", "com.github.javafaker.Faker"]>
<#if driverName == 'oracle'>
<#assign importList = importList + ["oracle.jdbc.OracleConnection"]>
<#if driverVersionName != 'ojdbc6' >
<#assign importList = importList + ["java.sql.Struct"]>


<#assign importList = importList + ["org.mockito.Mock", "org.mockito.Mockito"]>
<#if junit == 'junit5'>
<#assign importList = importList + ["org.junit.jupiter.api.extension.ExtendWith", "org.mockito.junit.jupiter.MockitoExtension", "org.junit.jupiter.api.Assertions", "org.junit.jupiter.api.BeforeEach", "org.junit.jupiter.api.Test"]>
<#else>
<#assign importList = importList + ["org.junit.runner.RunWith", "org.mockito.runners.MockitoJUnitRunner", "org.junit.Assert", "org.junit.Before", "org.junit.Test"]>

<#assign importList = importList + ["org.mockito.ArgumentCaptor", "org.mockito.Captor", "org.mockito.Mock", "org.mockito.Mockito"]>

<#list importSort(importList) as import>
<#if previousImportMatch?? && !import?starts_with(previousImportMatch)>


import ${import};
<#assign previousImportMatch = import?keep_before_last(".") >

<#if importList?has_content>


<#if junit == 'junit5'>
@ExtendWith(MockitoExtension.class)
<#else>
@RunWith(MockitoJUnitRunner.class)

class ${prefixUtilityName}ObjectUtilImplTest {

<#if driverName == 'oracle'>

    @Mock
    private OracleConnection oracleConnectionMock;
<#if driverVersionName != 'ojdbc6' >

    @Mock
    private Struct structMock;



    @Mock
    private Connection connectionMock;

    Faker faker;

    private ${prefixUtilityName}ObjectUtil objectUtil;

    @<#if junit == 'junit5'>BeforeEach<#else>Before
    void setUp() {
        faker = new Faker();
        objectUtil = new ${prefixUtilityName}ObjectUtilImpl();
    }
<#if driverName == 'oracle' && driverVersionName != 'ojdbc6' >

    @Test
    void testProcessObject() throws SQLException {
        Object[] objects = new Object[0];
        String nameValue = faker.internet().uuid();

        Mockito.when(connectionMock.unwrap(OracleConnection.class)).thenReturn(oracleConnectionMock);
        Mockito.when(oracleConnectionMock.createStruct(Mockito.anyString(), Mockito.same(objects))).thenReturn(structMock);

        Object result = objectUtil.process(connectionMock, nameValue, objects);

        <#if junit == 'junit5'>Assertions<#else>Assert.assertNotNull(result);
        <#if junit == 'junit5'>Assertions<#else>Assert.assertSame(structMock, result);

        Mockito.verify(oracleConnectionMock).<#if driverVersionName == 'ojdbc6' >createARRAY<#else>createOracleArray(Mockito.eq(nameValue), Mockito.same(objects));
    }


    @Test<#if junit != 'junit5'>(expected = SQLException.class)
    void testProcessObjectError()<#if junit != 'junit5' || driverName == 'oracle'> throws SQLException {
        Object[] objects = new Object[0];
        String nameValue = faker.internet().uuid();
<#if driverName == 'oracle' >

        Mockito.when(connectionMock.unwrap(OracleConnection.class)).thenReturn(oracleConnectionMock);
<#if driverVersionName != 'ojdbc6' >
        Mockito.when(oracleConnectionMock.createStruct(Mockito.anyString(), Mockito.same(objects))).thenThrow(new RuntimeException());



        <#if junit == 'junit5'>Assertions.assertThrows(SQLException.class,() -> objectUtil.process(connectionMock, nameValue, objects)<#if junit == 'junit5'>);
    }
<#if driverName != 'oracle' >

    @Test<#if junit != 'junit5'>(expected = SQLException.class)
    void should_check_struct_not_supported()<#if junit != 'junit5'> throws SQLException {
        Object[] objects = new Object[0];
        String nameValue = faker.internet().uuid();
        <#if junit == 'junit5'>Assertions.assertThrows(SQLException.class,() -> objectUtil.process(connectionMock, nameValue, objects)<#if junit == 'junit5'>);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy