0

Ich verwende einen SOAP Webservice mit Spring WebServiceTemplate. Während der Ausführung der JUnit stehe ich vor einem Problem.
JUnit für Spring WebServiceTemplate funktioniert nicht

Kundencenter Klasse:

@Service 
public class MyWsClient extends WebServiceGatewaySupport { 

@Autowired 
private WebServiceTemplate webServiceTemplate; 

public String getData(...) throws Exception{ 
    ... 
    SampleResponse response = (SampleResponse) webServiceTemplate.marshalSendAndReceive(request); 
    ... 
    return response.getData(); 
}} 

My Test-Klasse:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:applicationContext-junit.xml"}) 
public class MyWsClientTest { 

@Autowired 
ApplicationContext applicationContext; 

@InjectMocks 
MyWsClient myWsClient; 

private MockWebServiceServer mockServer; 

@Before 
public void init() { 
    MockitoAnnotations.initMocks(this); 
    mockServer = MockWebServiceServer.createServer(applicationContext); 
} 

@Test 
public void testGetData() throws Exception { 

    Source expectedRequestPayload = new StringSource(
      "<customerCountRequest xmlns=\"http://springframework.org/spring-ws/test\" />"); 
    Source responsePayload = new StringSource(
      "<customerCountResponse xmlns='http://springframework.org/spring-ws/test'>" 
        + "<customerCount>10</customerCount>" + "</customerCountResponse>"); 
    mockServer.expect(RequestMatchers.payload(expectedRequestPayload)).andRespond(ResponseCreators.withPayload(responsePayload)); 

    String data = myWsClient.getData(...); 
    assertNotNull(data); 
    mockServer.verify(); 
}} 

Ausgabe:
Wenn ich meine Testfall ausführen ich erhalte Nullpointer für Zeile " webServiceTemplate.marshalSendAndReceive (Anfrage) "in meinem Clie Klasse.
Das webServiceTemplate wird als null angezeigt.
Irgendeine Idee, was ich falsch mache?

Antwort

0

Ich löste es, indem ich @InjectMocks auf @Autowired auf dem "MyWsClient myWsClient" änderte;

Verwandte Themen