Why is the TestRestTemplate postForEntity object body null - java

So my problem is this essentially:
http://i.imgur.com/rE0z7Um.png
Here is the class that throws the error once the response is called in the #Test method:
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#ComponentScan
#Import(RepositoryRestMvcAutoConfiguration.class)
public class AjaxControllerTest {
static final String DEFAULT_URI = "/api/some_function";
SomeObjects someObjects;
#Autowired
private TestRestTemplate restTemplate;
#Before
public void setUp() {
SortedSet<SomeObject> someObjectsSet = new TreeSet<>();
someObjectsSet.add(new SomeObject());
someObjectsSet.add(new SomeObject());
someObjects = new SomeObjects(someObjectsSet);
}
#Test
public void testGetFullJsonSuccessful() {
ResponseEntity<SomeObjects> response = this.restTemplate.postForEntity(DEFAULT_URI, someObjects, SomeObjects.class);
assertThat(response).isNotNull();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody()).isNotNull();
}
}
Here is the AjaxController class function:
#PostMapping(value = "/api/some_function", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> someFunction(#RequestBody SomeObjects someObjects, Errors errors) {
AjaxResponseBody result = new AjaxResponseBody();
if (errors.hasErrors()) {
result.setMsg(errors.getAllErrors().stream().map(x -> x.getDefaultMessage()).collect(Collectors.joining(";")));
return ResponseEntity.badRequest().body(result);
}
Set<SomeObject> someObjectsSet = new TreeSet<>();
if (someObjectsSet.isEmpty()) {
result.setMsg("No data found.");
} else {
result.setMsg("Success");
}
result.setResult(someObjectsSet);
return ResponseEntity.ok(result);
}
Ajax Response object:
public class AjaxResponseBody {
private String msg;
private Set<SomeObject> result;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Set<SomeObject> getResult() {
return result;
}
public void setResult(Set<SomeObject> result) {
this.result = result;
}
}
and Involved POJOs:
public class SomeObject implements Comparable<SomeObject> {
private String str;
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SomeObject that = (SomeObject) o;
return str != null ? str.equals(that.str) : that.str == null;
}
#Override
public int hashCode() {
return str != null ? str.hashCode() : 0;
}
#Override
public int compareTo(SomeObject o) {
return 0;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
public class SomeObjects {
private SortedSet<SomeObject> someObjects;
public SomeObjects(SortedSet<SomeObject> someObjects) {
this.setSomeObjects(someObjects);
}
public SomeObjects() {
}
public SortedSet<SomeObject> getSomeObjects() {
return someObjects;
}
public void setSomeObjects(SortedSet<SomeObject> someObjects) {
this.someObjects = someObjects;
}
}
Sorry for the code spam, but I want to be complete and I can't trace the problem myself. If I get an answer I will edit and trim down the unimportant code.

Related

Java send class parameters by only one object

It is possible to create an Object as "only for parameters"?. For example:
class MyClass {
public String a;
public Number b;
public MyClass(Object params) {
this.a = params.a !== null ? params.a : "default";
this.b = params.b !== null ? params.b : 0;
}
}
void main() {
MyClass myclass1 = new MyClass(new Object() {
String a = "hey";
});
MyClass myclass2 = new MyClass(new Object() {
Number b = 123;
});
MyClass myclass3 = new MyClass(new Object() {
String a = "!!!";
Number b = 5;
});
}
Obviously this code doesn't work, I tried a lot of ways trying to replicate it, maybe with Templates (Generic)?
The expected results will be:
myclass1.a == "hey";
myclass1.b == 0;
myclass2.a == "default";
myclass2.b == 123;
myclass3.a == "!!!";
myclass3.b == 5;
You could use a Builder pattern to create required instance:
public final class MyClass {
private final String str;
private final Number number;
public static Builder builder() {
return new Builder();
}
private MyClass(Builder builder) {
str = builder.str;
number = builder.number;
}
public String getStr() {
return str;
}
public Number getNumber() {
return number;
}
public static final class Builder {
private String str = "default";
private Number number = 0;
private Builder() {
}
public MyClass build() {
return new MyClass(this);
}
public Builder str(String str) {
this.str = str;
return this;
}
public Builder number(Number number) {
this.number = number;
return this;
}
}
}
Demo:
public static void main(String... args) {
MyClass myclass1 = MyClass.builder().str("hey").build();
MyClass myclass2 = MyClass.builder().number(123).build();
MyClass myclass3 = MyClass.builder().str("!!!").number(5).build();
}
In case you do not want to use Builder pattern, you could use class override:
public class MyClass {
public String getStr() {
return "default";
}
public Number getNumber() {
return 0;
}
}
public static void main(String... args) throws IOException {
MyClass myclass1 = new MyClass() {
#Override
public String getStr() {
return "hey";
}
};
MyClass myclass2 = new MyClass() {
#Override
public String getStr() {
return "hey";
}
#Override
public Number getNumber() {
return 123;
}
};
MyClass myclass3 = new MyClass() {
#Override
public String getStr() {
return "!!!";
}
#Override
public Number getNumber() {
return 5;
}
};
}

How can I define intercept-url dynamically using Database that load them in runtime not just when start web application?

I've been working on a spring security recently and I know how I can define intercept-url (in Spring Security) dynamically using a Database.
but i need restart my web application to load defined intercept-url from database. But i need to load when i add a new intercept-url to database.
#Component
public class FilterInvocationServiceSecurityMetadataSourceBeanPostProcessor implements BeanPostProcessor {
#Autowired
private FilterInvocationServiceSecurityMetadataSource metadataSource;
#Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof FilterInvocationSecurityMetadataSource) {
return metadataSource;
}
if(bean instanceof FilterChainProxy.FilterChainValidator) {
return new FilterChainProxy.FilterChainValidator() {
#Override
public void validate(FilterChainProxy filterChainProxy) {
}
};
}
return bean;
}
#Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
#Component("filterInvocationServiceSecurityMetadataSource")
public class FilterInvocationServiceSecurityMetadataSource implements FilterInvocationSecurityMetadataSource, InitializingBean{
private FilterInvocationSecurityMetadataSource delegate;
private RequestConfigMappingService requestConfigMappingService;
private SecurityExpressionHandler<FilterInvocation> expressionHandler;
#Autowired
public FilterInvocationServiceSecurityMetadataSource(CustomWebSecurityExpressionHandler expressionHandler,
RequestConfigMappingService filterInvocationService) {
this.expressionHandler = expressionHandler;
this.requestConfigMappingService = filterInvocationService;
}
public Collection<ConfigAttribute> getAllConfigAttributes() {
return this.delegate.getAllConfigAttributes();
}
public Collection<ConfigAttribute> getAttributes(Object object) {
return this.delegate.getAttributes(object);
}
public boolean supports(Class<?> clazz) {
return this.delegate.supports(clazz);
}
#Override
public void afterPropertiesSet() throws Exception {
List<RequestConfigMapping> requestConfigMappings = requestConfigMappingService.getRequestConfigMappings();
LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>(requestConfigMappings.size());
for(RequestConfigMapping requestConfigMapping : requestConfigMappings) {
RequestMatcher matcher = requestConfigMapping.getMatcher();
requestMap.put(matcher,requestConfigMapping.getAttributes());
}
this.delegate = new ExpressionBasedFilterInvocationSecurityMetadataSource(requestMap, expressionHandler);
}
}
#Repository("requestConfigMappingService")
public class JdbcRequestConfigMappingService implements RequestConfigMappingService {
private SecurityFilterMetaDataService securityFilterMetaDataService;
#Autowired
public JdbcRequestConfigMappingService(SecurityFilterMetaDataService securityFilterMetaDataService) {
if (securityFilterMetaDataService == null) {
throw new IllegalArgumentException("securityFilterMetaDataService cannot be null");
}
this.securityFilterMetaDataService = securityFilterMetaDataService;
}
#Override
public List<RequestConfigMapping> getRequestConfigMappings() {
String pattern = "";
String expressionString = "";
List<SecurityFilterMetaData> securityFilterMetaDataList = securityFilterMetaDataService.getByAscOrder("sortOrder");
List<RequestConfigMapping> requestConfigMappings = new ArrayList<>();
for (SecurityFilterMetaData securityFilterMetaData : securityFilterMetaDataList) {
pattern = securityFilterMetaData.getAntPattern();
expressionString = securityFilterMetaData.getExpression();
AntPathRequestMatcher matcher = new AntPathRequestMatcher(pattern);
requestConfigMappings.add(new RequestConfigMapping(matcher, new SecurityConfig(expressionString)));
}
return requestConfigMappings;
}
private static final class RequestConfigMappingMapper implements RowMapper<RequestConfigMapping> {
#Override
public RequestConfigMapping mapRow(ResultSet rs, int rowNum) throws SQLException {
String pattern = rs.getString("ant_pattern");
String expressionString = rs.getString("expression");
AntPathRequestMatcher matcher = new AntPathRequestMatcher(pattern);
return new RequestConfigMapping(matcher, new SecurityConfig(expressionString));
}
}
}
public interface RequestConfigMappingService {
List<RequestConfigMapping> getRequestConfigMappings();
}
public final class RequestConfigMapping {
private final RequestMatcher matcher;
private final Collection<ConfigAttribute> attributes;
public RequestConfigMapping(RequestMatcher matcher, ConfigAttribute attribute) {
this(matcher, Collections.singleton(attribute));
}
public RequestConfigMapping(RequestMatcher matcher, Collection<ConfigAttribute> attributes) {
if (matcher == null) {
throw new IllegalArgumentException("matcher cannot be null");
}
Assert.notEmpty(attributes, "attributes cannot be null or emtpy");
this.matcher = matcher;
this.attributes = attributes;
}
public RequestMatcher getMatcher() {
return matcher;
}
public Collection<ConfigAttribute> getAttributes() {
return attributes;
}
}
At last I found answer. FilterInvocationServiceSecurityMetadataSource must change.
NOTE: Keep in mind that getAttributes will be invoked for every request that Spring Security intercepts so you will most likely want some sort of caching.
#Component("filterInvocationServiceSecurityMetadataSource")
public class FilterInvocationServiceSecurityMetadataSource implements FilterInvocationSecurityMetadataSource, InitializingBean{
private FilterInvocationSecurityMetadataSource delegate;
private RequestConfigMappingService requestConfigMappingService;
private SecurityExpressionHandler<FilterInvocation> expressionHandler;
#Autowired
public FilterInvocationServiceSecurityMetadataSource(CustomWebSecurityExpressionHandler expressionHandler,
RequestConfigMappingService filterInvocationService) {
this.expressionHandler = expressionHandler;
this.requestConfigMappingService = filterInvocationService;
}
public Collection<ConfigAttribute> getAllConfigAttributes() {
return this.delegate.getAllConfigAttributes();
}
public Collection<ConfigAttribute> getAttributes(Object object) {
List<RequestConfigMapping> requestConfigMappings = requestConfigMappingService.getRequestConfigMappings();
LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>(requestConfigMappings.size());
for(RequestConfigMapping requestConfigMapping : requestConfigMappings) {
RequestMatcher matcher = requestConfigMapping.getMatcher();
requestMap.put(matcher,requestConfigMapping.getAttributes());
}
this.delegate = new ExpressionBasedFilterInvocationSecurityMetadataSource(requestMap, expressionHandler);
return this.delegate.getAttributes(object);
}
public boolean supports(Class<?> clazz) {
return this.delegate.supports(clazz);
}
#Override
public void afterPropertiesSet() throws Exception {
List<RequestConfigMapping> requestConfigMappings = requestConfigMappingService.getRequestConfigMappings();
LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>>(requestConfigMappings.size());
for(RequestConfigMapping requestConfigMapping : requestConfigMappings) {
RequestMatcher matcher = requestConfigMapping.getMatcher();
requestMap.put(matcher,requestConfigMapping.getAttributes());
}
this.delegate = new ExpressionBasedFilterInvocationSecurityMetadataSource(requestMap, expressionHandler);
}
}

how to work with WritableComparator Hadoop

Below are my code snippet for using WritableComparator, but it does not work
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
public class MovieComparator extends WritableComparator{
public MovieComparator(){
super(Movie.class);
}
#Override
public int compare(WritableComparable o,WritableComparable o2){
System.out.println("in compare");
Movie m = (Movie)o;
Movie m2 = (Movie)o2;
System.out.println(m.compareTo(m2));
return m.movieId.compareTo(m2.movieId);
}
}
public class Movie implements WritableComparable {
Text movieId;
Text movieTitle;
public Movie(Text movieId, Text movieTitle) {
this.movieId = movieId;
this.movieTitle = movieTitle;
}
public Movie(){
}
public String getMovieId() {
return movieId.toString();
}
public void setMovieId(String movieId) {
this.movieId = new Text(movieId);
}
public String getMovieTitle() {
return movieTitle.toString();
}
public void setMovieTitle(String movieTitle) {
this.movieTitle = new Text(movieTitle);
}
#Override
public void readFields(DataInput in) throws IOException {
//movieId = in.read;
movieId.readFields(in);
movieTitle.readFields(in);
}
#Override
public void write(DataOutput out) throws IOException {
//out.writeUTF(movieId);
//out.writeUTF(movieTitle);
movieId.write(out);
movieTitle.write(out);
}
#Override
public int compareTo(Movie o) {
// System.out.println("in compareTo");
int res=movieTitle.compareTo(o.movieTitle);
return res;
}
#Override
public int hashCode(){
return movieId.hashCode();
}
#Override
public boolean equals(Object o){
Movie m=(Movie)o;
return movieId.equals(m.movieId);
}
#Override
public String toString(){
return movieTitle.toString();
}
}
In driver class I am setting the comparator by below line
job.setSortComparatorClass(MovieComparator.class);
Can any body tell me where I am wrong in this at it gives exception below
14/09/08 14:17:03 WARN mapred.LocalJobRunner: job_local_0001
java.io.IOException: Spill failed
at org.apache.hadoop.mapred.MapTask$MapOutputBuffer.collect(MapTask.java:1029)
at org.apache.hadoop.mapred.MapTask$NewOutputCollector.write(MapTask.java:691)
at org.apache.hadoop.mapreduce.TaskInputOutputContext.write(TaskInputOutputContext.java:80)
at com.impetus.MovieMapper.map(MovieMapper.java:44)
at com.impetus.MovieMapper.map(MovieMapper.java:1)
at org.apache.hadoop.mapreduce.Mapper.run(Mapper.java:144)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:764)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:370)
at org.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:212)
I found the issue that Instead of using super(Movie.class), I will have to use super(Movie.class,true). As by sending true, WritableComparator will instantiate the object other wise it will pass null in compare method

java- error when i am trying to convert xml response into pojo using xstream

I am getting an error for the following code in a java web app--
XStream xstream = new XStream();
apiresponse myClassObject;
myClassObject= xstream.fromXML(resp);
The error is shown for the line of code just above this line--
error="Type mismatch- cannot convert from Object to apiresponse"
Given below is the XML that I have to parse---
<apiresponse version="1" xmlns="http://ahrefs.com/schemas/api/links/1">
<resultset_links count="2">
<result>
<source_url>http://ahrefs.com/robot/</source_url>
<destination_url>http://blog.ahrefs.com/</destination_url>
<source_ip>50.22.24.236</source_ip>
<source_title>Ahrefs – backlinks research tool</source_title>
<visited>2011-08-31T07:56:53Z</visited>
<anchor>Blog</anchor>
<rating>257.674000</rating>
<link_type>text</link_type>
<is_nofollow>false</is_nofollow>
</result>
<result>
<source_url>http://apps.vc/</source_url>
<destination_url>http://ahrefs.com/robot/</destination_url>
<source_ip>64.20.55.86</source_ip>
<source_title>Device info</source_title>
<visited>2011-08-27T18:59:31Z</visited>
<anchor>http://ahrefs.com/robot/</anchor>
<rating>209.787100</rating>
<link_type>text</link_type>
<is_nofollow>false</is_nofollow>
</result>
</resultset_links>
</apiresponse>
I have created the following java classes to obtain data from above xml---
package com.arvindikchari.linkdatasmith.domain;
final public class apiresponse {
protected resultset_links rlinks;
public apiresponse() {
}
public resultset_links getRlinks()
{
return rlinks;
}
public setRlinks(resultset_links rlinks)
{
this.rlinks=rlinks;
}
}
final public class resultset_links {
protected List<result> indiv_result = new ArrayList<result>();
public resultset_links() {
}
public List<result> getIndiv_result()
{
return List;
}
public void setIndiv_result(List<result> indiv_result)
{
this.indiv_result=indiv_result;
}
}
final public class result {
protected String source_url;
protected String destination_url;
protected String source_ip;
protected String source_title;
protected String visited;
protected String anchor;
protected String rating;
protected String link_type;
public result() {
}
public String getSource_url()
{
return source_url;
}
public void setSource_url(String source_url)
{
this.source_url=source_url;
}
public String getDestination_url()
{
return destination_url;
}
public void setDestination_url(String destination_url)
{
this.destination_url=destination_url;
}
public String getSource_ip()
{
return source_ip;
}
public void setSource_ip(String source_ip)
{
this.source_ip=source_ip;
}
public String getSource_title()
{
return source_title;
}
public void setSource_title(String source_title)
{
this.source_title=source_title;
}
public String getVisited()
{
return visited;
}
public void setVisited(String visited)
{
this.visited=visited;
}
public String getAnchor()
{
return anchor;
}
public void setAnchor(String anchor)
{
this.anchor=anchor;
}
public String getRating()
{
return rating;
}
public void setRating(String rating)
{
this.rating=rating;
}
public String getLink_type()
{
return link_type;
}
public void setLink_type(String link_type)
{
this.link_type=link_type;
}
}
What am I doing wrong here?
You have many errors, but the one corresponding to your message is you have to cast the result of xstream.fromXML to an apiresponse' object :
apiresponse result = (apiresponse)xstream.fromXML(resp);
Moreover, the code you provided (the Java classes) do not compile, there are many errors.
Here are some improvements :
Result.java :
#XStreamAlias("result")
public class Result {
protected String source_url;
protected String destination_url;
protected String source_ip;
protected String source_title;
protected String visited;
protected String anchor;
protected String rating;
protected String link_type;
protected Boolean is_nofollow;
public Result() {
}
public String getSource_url()
{
return source_url;
}
public void setSource_url(String source_url)
{
this.source_url=source_url;
}
public String getDestination_url()
{
return destination_url;
}
public void setDestination_url(String destination_url)
{
this.destination_url=destination_url;
}
public String getSource_ip()
{
return source_ip;
}
public void setSource_ip(String source_ip)
{
this.source_ip=source_ip;
}
public String getSource_title()
{
return source_title;
}
public void setSource_title(String source_title)
{
this.source_title=source_title;
}
public String getVisited()
{
return visited;
}
public void setVisited(String visited)
{
this.visited=visited;
}
public String getAnchor()
{
return anchor;
}
public void setAnchor(String anchor)
{
this.anchor=anchor;
}
public String getRating()
{
return rating;
}
public void setRating(String rating)
{
this.rating=rating;
}
public String getLink_type()
{
return link_type;
}
public void setLink_type(String link_type)
{
this.link_type=link_type;
}
public Boolean getIs_nofollow() {
return is_nofollow;
}
public void setIs_nofollow(Boolean is_nofollow) {
this.is_nofollow = is_nofollow;
}
ResultsetLinks.java :
#XStreamAlias("resultset_links")
public class ResultsetLinks {
#XStreamImplicit(itemFieldName="result")
protected List<Result> indivResult = new ArrayList<Result>();
public ResultsetLinks() {
}
public List<Result> getResult()
{
return indivResult;
}
public void setResult(List<Result> indiv_result)
{
this.indivResult =indiv_result;
}
}
ApiResponse.java :
#XStreamAlias("apiresponse")
public class ApiResponse {
#XStreamAlias("resultset_links")
protected ResultsetLinks rlinks;
public ApiResponse() {
}
public ResultsetLinks getRlinks()
{
return rlinks;
}
public void setRlinks(ResultsetLinks rlinks)
{
this.rlinks=rlinks;
}
}
And finally your code to unmarshall the XML :
XStream xstream = new XStream();
xstream.processAnnotations(ApiResponse.class);
xstream.processAnnotations(ResultsetLinks.class);
xstream.processAnnotations(Result.class);
ApiResponse result = (ApiResponse)xstream.fromXML(resp);
All this code is working fine with Xstream 1.4.2
Try to follow Sun's coding convention for your classes name, attributes names, etc...
Use XstreamAliases to adapt the Java class name to the XML name.

JUnit test of the same object

I want to make a unit test suite of the same object with same variable but different values. However if the object get the same name (created by this.setName("testlaunch"); (we must have the name of a method tested by JUnit), it runs only one test.
If i don't write this.setName("testlaunch"); it complains saying junit.framework.AssertionFailedError: TestCase.fName cannot be null.
I don't know what to do...
public class LanceurRegleGestion extends TestSuite
{
public static Test suite()
{
Class maClasse = null;
TestSuite suite = new TestSuite();
String filtre = ".*.xml";
// on compile le pattern pour l'expression réguliere
Pattern p = Pattern.compile(filtre);
String path = "D:/Documents/workspace/Solipsisme/src/ReglesGestion/XML/";
// on liste les fichiers du repertoire
String [] u = new File(path).list();
// on parcours la liste de fichier
System.out.println("Initialisation");
for (int i=0; i
et le code de l'objet serialisé
public class Application extends TestCase {
private String nomappli;
private String id2_1;
private String id3_1;
private String id4_1;
private String id2_2;
private String id3_2;
private String id4_2;
private String id5_2;
private String id6_2;
private String id7_2;
private String id8_2;
private String id9_2;
private String id2_3;
private String id3_3;
private String id4_3;
private String id2_4;
private String id3_4;
private String id4_4;
private String id2_5;
private String id3_5;
private String id4_5;
private String id5_5;
private String id6_5;
private String id7_5;
private static Selenium selenium;
public Application(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2,String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4,String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5)
{
this.setName("testlaunch");
this.nomappli = nomappli;
this.id2_1 = id2_1;
this.id3_1 = id3_1;
this.id4_1 = id4_1;
this.id2_2 = id2_2;
this.id3_2 = id3_2;
this.id4_2 = id4_2;
this.id5_2 = id5_2;
this.id6_2 = id6_2;
this.id7_2 = id7_2;
this.id8_2 = id8_2;
this.id9_2 = id9_2;
this.id2_3 = id2_3;
this.id3_3 = id3_3;
this.id4_3 = id4_3;
this.id2_4 = id2_4;
this.id3_4 = id3_4;
this.id4_4 = id4_4;
this.id2_5 = id2_5;
this.id3_5 = id3_5;
this.id4_5 = id4_5;
this.id5_5 = id5_5;
this.id6_5 = id6_5;
this.id7_5 = id7_5;
}
public Application(){
}
public String toString()
{
return getNomappli();
}
public void setNomappli(String nomappli)
{
this.nomappli = nomappli;
}
public String getNomappli()
{
return this.nomappli;
}
public void setId2_1(String id2_1)
{
this.id2_1 = id2_1;
}
public String getId2_1()
{
return this.id2_1;
}
public void setId3_1(String id3_1)
{
this.id3_1 = id3_1;
}
public String getId3_1()
{
return this.id3_1;
}
public void setId4_1(String id4_1)
{
this.id4_1 = id4_1;
}
public String getId4_1()
{
return this.id4_1;
}
public void setId2_2(String id2_2)
{
this.id2_2 = id2_2;
}
public String getId2_2()
{
return this.id2_2;
}
public void setId3_2(String id3_2)
{
this.id3_2 = id3_2;
}
public String getId3_2()
{
return this.id3_2;
}
public void setId4_2(String id4_2)
{
this.id4_2 = id4_2;
}
public String getId4_2()
{
return this.id4_2;
}
public void setId5_2(String id5_2)
{
this.id5_2 = id5_2;
}
public String getId5_2()
{
return this.id5_2;
}
public void setId6_2(String id6_2)
{
this.id6_2 = id6_2;
}
public String getId6_2()
{
return this.id6_2;
}
public void setId7_2(String id7_2)
{
this.id7_2 = id7_2;
}
public String getId7_2()
{
return this.id7_2;
}
public void setId8_2(String id8_2)
{
this.id8_2 = id8_2;
}
public String getId8_2()
{
return this.id8_2;
}
public void setId9_2(String id9_2)
{
this.id9_2 = id9_2;
}
public String getId9_2()
{
return this.id9_2;
}
public void setId2_3(String id2_3)
{
this.id2_3 = id2_3;
}
public String getId2_3()
{
return this.id2_3;
}
public void setId3_3(String id3_3)
{
this.id3_3 = id3_3;
}
public String getId3_3()
{
return this.id3_3;
}
public void setId4_3(String id4_3)
{
this.id4_3 = id4_3;
}
public String getId4_3()
{
return this.id4_3;
}
public void setId2_4(String id2_4)
{
this.id2_4 = id2_4;
}
public String getId2_4()
{
return this.id2_4;
}
public void setId3_4(String id3_4)
{
this.id3_4 = id3_4;
}
public String getId3_4()
{
return this.id3_4;
}
public void setId4_4(String id4_4)
{
this.id4_4 = id4_4;
}
public String getId4_4()
{
return this.id4_4;
}
public void setId2_5(String id2_5)
{
this.id2_5 = id2_5;
}
public String getId2_5()
{
return this.id2_5;
}
public void setId3_5( String id3_5)
{
this.id3_5 = id3_5;
}
public String getId3_5()
{
return this.id3_5;
}
public void setId4_5(String id4_5)
{
this.id4_5 = id4_5;
}
public String getId4_5()
{
return this.id4_5;
}
public void setId5_5(String id5_5)
{
this.id5_5 = id5_5;
}
public String getId5_5()
{
return this.id5_5;
}
public void setId6_5(String id6_5)
{
this.id6_5 = id6_5;
}
public String getId6_5()
{
return this.id6_5;
}
public void setId7_5(String id7_5)
{
this.id7_5 = id7_5;
}
public String getId7_5()
{
return this.id7_5;
}
public void setSelenium(Selenium selenium)
{
this.selenium = selenium;
}
public Selenium getSelenium()
{
return this.selenium;
}
public final static void login()
{
selenium.open("apj/ident");
selenium.type("username", "hsuzumiya-cp");
selenium.type("password", "1");
selenium.click("enterButton");
selenium.waitForPageToLoad("9999999");
}
public void testlaunch()
{
generique(this.nomappli,this.id2_1,this.id3_1,this.id4_1,this.id2_2,this.id3_2,this.id4_2,this.id5_2,this.id6_2,this.id7_2,this.id8_2,this.id9_2,this.id2_3,this.id3_3,this.id4_3,this.id2_4,this.id3_4,this.id4_4,this.id2_5,this.id3_5,this.id4_5,this.id5_5,this.id6_5,this.id7_5);
}
public void setUp() throws Exception
{
System.out.println("Initialisation");
selenium = new DefaultSelenium("127.0.0.1",4444,"*iexplore", "http://hsuzumiya/");
selenium.start();
selenium.setTimeout("90000");
selenium.setSpeed("500");
login();
}
public void generique(String nomappli,String id2_1,String id3_1,String id4_1,String id2_2,String id3_2,String id4_2,
String id5_2,String id6_2,String id7_2,String id8_2,String id9_2,String id2_3,String id3_3,String id4_3,String id2_4,
String id3_4,String id4_4,String id2_5, String id3_5,String id4_5,String id5_5,String id6_5,String id7_5
)
{
System.out.println(nomappli);
selenium.click("valider");
selenium.waitForPageToLoad("30000");
selenium.click("validertout");
}
public final void tearDown() throws Exception
{
System.out.println("Killing session");
selenium.stop();
}
}
Being new to junit, I stumbled upon this question hoping to solve a problem I had getting the same message. Through further research, I found that it is necessary to pass the name of the test function you want to invoke through addTest to the constructor of the test case class. A simple (and useless, other than illustration) example follows:
JunitTestCases.java
import junit.framework.TestCase;
public class JunitTestCases extends TestCase {
public JunitTestCases(String fnName) {
super(fnName);
}
public void testA() {
assertTrue("assertTrue failed", true);
}
}
JunitTestSuite.java:
import junit.framework.*;
public class JunitTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new JunitTestCases("testA"));
return suite;
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
When I compiled with:
javac -cp .:path/to/junit-X.X.X.jar JunitTestSuite.java
and ran with
java -cp .:path/to/junit-X.X.X.jar JunitTestSuite
this worked with no errors, with junit giving me an OK message.

Categories

Resources