Skip to content
The Chat Archive
claude-3-7-sonnet-20250219· Anthropic LMArena Human Preference 140K

kannst du mir für folgende Java-Klasse einen JUnit 5 Test schreiben unter der Verwendung…

  1. User

    kannst du mir für folgende Java-Klasse einen JUnit 5 Test schreiben unter der Verwendung von // given // when // then und AssertJ? Danke!

    package com.devk.vpu.leben.vwb.vwbkachelservice.service;

    import com.devk.vpu.leben.vwb.vwbkachelservice.client.STSClient;

    import java.util.Optional;

    import jakarta.xml.soap.SOAPException; import jakarta.xml.soap.SOAPMessage;

    import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j;

    import org.springframework.stereotype.Service;

    @Slf4j @Service @RequiredArgsConstructor public class STSService {

    private final STSClient client;

    public Optional getStsToken() { return client.getStsResponse().map(this::extractStsToken); }

    private String extractStsToken(SOAPMessage response) { // ACHTUNG! Fehlermeldungen haben "MeldungId" und nicht "MeldungID", daher unten der Catch auf NPE! var meldungId = getNodeValue(response, "MeldungID"); if (meldungId != null && meldungId.equals("00000")) { return getNodeValue(response, "wsc:Identifier"); } return null; }

    private String getNodeValue(SOAPMessage soapResponse, String identifier) { try { return soapResponse.getSOAPBody().getElementsByTagName(identifier).item(0).getFirstChild().getNodeValue(); } catch (SOAPException | NullPointerException e) { log.error("Identifier '{}' not in SOAP response! Reason: {}", identifier, e.getMessage()); } return null; } }

  2. Assistant

    Hier ist ein JUnit 5 Test für die STSService-Klasse unter Verwendung von AssertJ und dem "given/when/then"-Muster:

    package com.devk.vpu.leben.vwb.vwbkachelservice.service;
    
    import com.devk.vpu.leben.vwb.vwbkachelservice.client.STSClient;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mock;
    import org.mockito.junit.jupiter.MockitoExtension;
    
    import jakarta.xml.soap.SOAPBody;
    import jakarta.xml.soap.SOAPException;
    import jakarta.xml.soap.SOAPMessage;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    import java.util.Optional;
    
    import static org.assertj.core.api.Assertions.assertThat;
    import static org.mockito.Mockito.*;
    
    @ExtendWith(MockitoExtension.class)
    class STSServiceTest {
    
        @Mock
        private STSClient client;
    
        @Mock
        private SOAPMessage soapMessage;
    
        @Mock
        private SOAPBody soapBody;
    
        @Mock
        private NodeList nodeList;
    
        @Mock
        private Element element;
    
        @Mock
        private Node node;
    
        @InjectMocks
        private STSService stsService;
    
        @BeforeEach
        void setUp() throws SOAPException {
            when(soapMessage.getSOAPBody()).thenReturn(soapBody);
        }
    
        @Test
        void shouldReturnStsTokenWhenResponseIsValid() throws SOAPException {
            // given
            String expectedToken = "test-token-123";
            mockNodeValue("MeldungID", "00000");
            mockNodeValue("wsc:Identifier", expectedToken);
            when(client.getStsResponse()).thenReturn(Optional.of(soapMessage));
    
            // when
            Optional<String> result = stsService.getStsToken();
    
            // then
            assertThat(result).isPresent();
            assertThat(result.get()).isEqualTo(expectedToken);
        }
    
        @Test
        void shouldReturnEmptyWhenClientReturnsEmpty() {
            // given
            when(client.getStsResponse()).thenReturn(Optional.empty());
    
            // when
            Optional<String> result = stsService.getStsToken();
    
            // then
            assertThat(result).isEmpty();
        }
    
        @Test
        void shouldReturnEmptyWhenMeldungIdIsNotSuccess() throws SOAPException {
            // given
            mockNodeValue("MeldungID", "10001");
            when(client.getStsResponse()).thenReturn(Optional.of(soapMessage));
    
            // when
            Optional<String> result = stsService.getStsToken();
    
            // then
            assertThat(result).isEmpty();
        }
    
        @Test
        void shouldReturnEmptyWhenMeldungIdIsNull() throws SOAPException {
            // given
            mockNodeValueWithException("MeldungID");
            when(client.getStsResponse()).thenReturn(Optional.of(soapMessage));
    
            // when
            Optional<String> result = stsService.getStsToken();
    
            // then
            assertThat(result).isEmpty();
        }
    
        @Test
        void shouldReturnEmptyWhenIdentifierThrowsException() throws SOAPException {
            // given
            mockNodeValue("MeldungID", "00000");
            mockNodeValueWithException("wsc:Identifier");
            when(client.getStsResponse()).thenReturn(Optional.of(soapMessage));
    
            // when
            Optional<String> result = stsService.getStsToken();
    
            // then
            assertThat(result).isEmpty();
        }
    
        private void mockNodeValue(String identifier, String value) throws SOAPException {
            when(soapBody.getElementsByTagName(identifier)).thenReturn(nodeList);
            when(nodeList.item(0)).thenReturn(element);
            when(element.getFirstChild()).thenReturn(node);
            when(node.getNodeValue()).thenReturn(value);
        }
    
        private void mockNodeValueWithException(String identifier) throws SOAPException {
            when(soapBody.getElementsByTagName(identifier)).thenReturn(nodeList);
            when(nodeList.item(0)).thenReturn(null);
        }
    }
    

    Möchtest du, dass ich den Code erkläre oder aufschlüssele?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.