Skip to content
Permalink
Browse files

How to write unit test

  • Loading branch information...
slem1 committed Oct 31, 2017
1 parent aa10cea commit fc8f53799df976ddb471c090a6a48dba0a26ea32
@@ -9,4 +9,8 @@
public class MyComponent {

public void print(){}

public String giveMeAValue(){
return "foo";
}
}
@@ -1,19 +1,14 @@
package fr.sle.testhowto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceMessageCallback;
import org.springframework.ws.client.core.WebServiceMessageExtractor;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapMessage;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@@ -24,14 +19,18 @@
@Transactional
public class MyEntityService {

@Autowired
private MyEntityJpaRepository myEntityJpaRepository;

@Autowired
private RestTemplate restTemplate;
private final MyEntityJpaRepository myEntityJpaRepository;

@Autowired
private WebServiceTemplate webServiceTemplate;
private final RestTemplate restTemplate;

private final WebServiceTemplate webServiceTemplate;

public MyEntityService(MyEntityJpaRepository myEntityJpaRepository, RestTemplate restTemplate, WebServiceTemplate webServiceTemplate) {
this.myEntityJpaRepository = myEntityJpaRepository;
this.restTemplate = restTemplate;
this.webServiceTemplate = webServiceTemplate;
}

public void createnewMyEntity() {
myEntityJpaRepository.save(new MyEntity());
@@ -41,12 +40,12 @@ public void createnewMyEntity() {
return myEntityJpaRepository.findAll();
}

public Map<String,String> getExternalData(){
public Map<String, String> getExternalData() {
return restTemplate.getForObject("http://mythirdpartyhost/rest/api/data", Map.class);
}


public void callASoapWebService(){
public void callASoapWebService() {

WebServiceMessageCallback requestCallback = message -> {

@@ -0,0 +1,20 @@
package fr.sle.testhowto;

import org.springframework.stereotype.Service;

/**
* @author slemoine
*/
@Service
public class MyOtherService {

private final MyComponent myComponent;

public MyOtherService(MyComponent myComponent) {
this.myComponent = myComponent;
}

public String doSomething(){
return myComponent.giveMeAValue();
}
}
@@ -0,0 +1,28 @@
package fr.sle.testhowto.test;

import fr.sle.testhowto.MyComponent;
import fr.sle.testhowto.MyOtherService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;

/**
* @author slemoine
*/
@RunWith(JUnit4.class)
public class MyOtherServiceUnitTest {

@Test
public void aTest() {

MyComponent myComponent = Mockito.mock(MyComponent.class);
Mockito.when(myComponent.giveMeAValue()).thenReturn("mock value");
MyOtherService myOtherService = new MyOtherService(myComponent);

String result = myOtherService.doSomething();

Assert.assertEquals("mock value", result);
}
}

0 comments on commit fc8f537

Please sign in to comment.
You can’t perform that action at this time.